-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathransomNote.js
More file actions
29 lines (27 loc) · 956 Bytes
/
Copy pathransomNote.js
File metadata and controls
29 lines (27 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Ransom Note.
*
* Determines whether `note` can be assembled from the characters available in
* `magazine`, where each character of the magazine may be used at most once.
*
* We tally the magazine's character counts, then "spend" one per character of
* the note; if any character is missing or runs out, the note is impossible.
*
* Time: O(note + magazine) - Space: O(distinct magazine characters)
*
* @param {string} note - The text we want to build.
* @param {string} magazine - The pool of available characters.
* @returns {boolean} `true` if `note` can be built from `magazine`.
*/
export function canConstruct(note, magazine) {
const available = new Map();
for (const char of magazine) {
available.set(char, (available.get(char) ?? 0) + 1);
}
for (const char of note) {
const remaining = available.get(char) ?? 0;
if (remaining === 0) return false;
available.set(char, remaining - 1);
}
return true;
}