Maximum Number of Balloons LeetCode solution in JavaScript

1
/**
2
* @param {string} text
3
* @return {number}
4
*/
5
var maxNumberOfBalloons = function (text) {
6
// 1. create hashmap with "balloon" letters
7
// 2. keep track of how many letters in text belong to "balloon"
8
// 3. account for fact that we need two "l" and "o" per "balloon" instance
9
// 4. then select all map values and get the minimum - this will be the max value
10

11
const balloonMap = {
12
b: 0,
13
a: 0,
14
l: 0,
15
o: 0,
16
n: 0,
17
};
18

19
for (const char of text) {
20
if (balloonMap[char] !== undefined) {
21
balloonMap[char] += 1;
22
}
23
}
24

25
const letterFreq = [];
26
for (const key in balloonMap) {
27
if (["l", "o"].includes(key)) {
28
letterFreq.push(Math.floor(balloonMap[key] / 2));
29
} else {
30
letterFreq.push(balloonMap[key]);
31
}
32
}
33
return Math.min(...letterFreq);
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0