383. Ransom Note

202410161741
tags: #hash-map

var canConstruct = function(ransomNote, magazine) {
	const map = new Map();
	for (const c of magazine) {
		map.set(c, (map.get(c) ?? 0) + 1);
	}
	for (const c of ransomNote) {
		if (!map.has(c)) {
			return false;
		}
		if (map.get(c) <= 0) {
			return false;
		}
		map.set(c, map.get(c) - 1);
	}
	return true;
};

Reference

https://leetcode.com/problems/ransom-note/