-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximumNumberOfBalloons.js
63 lines (53 loc) · 1.65 KB
/
maximumNumberOfBalloons.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
// You can use each character in text at most once.Return the maximum number of instances that can be formed.
/**
* @param {string} text
* @return {number}
*/
var maxNumberOfBalloons = function(text) {
const letters = text.split("");
const letterCount = {};
for (letter of letters) {
if (letterCount[letter]) {
letterCount[letter] = letterCount[letter] + 1;
} else {
letterCount[letter] = 1;
}
}
let balloonsCount =
Math.floor(
Math.min(
letterCount["b"] / 1,
letterCount["a"] / 1,
letterCount["l"] / 2,
letterCount["o"] / 2,
letterCount["n"] / 1
)
) || 0;
// let balloonsCount = 0;
// let notFound = false;
// while (!notFound) {
// if (
// letterCount["b"] >= 1 &&
// letterCount["a"] >= 1 &&
// letterCount["l"] >= 2 &&
// letterCount["o"] >= 2 &&
// letterCount["n"] >= 1
// ) {
// letterCount["b"] = letterCount["b"] - 1;
// letterCount["a"] = letterCount["a"] - 1;
// letterCount["l"] = letterCount["l"] - 2;
// letterCount["o"] = letterCount["o"] - 2;
// letterCount["n"] = letterCount["n"] - 1;
// balloonsCount++;
// } else {
// notFound = true;
// }
// }
// return balloonsCount;
return balloonsCount;
};
console.log(maxNumberOfBalloons("nlaebolko")); // 1
console.log(maxNumberOfBalloons("loonbalxballpoon")); //2
console.log(maxNumberOfBalloons("leetcode")); // 0
console.log(maxNumberOfBalloons("balon")); // 0