-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1189-maximum-number-of-balloons.cpp
32 lines (28 loc) · 1.09 KB
/
1189-maximum-number-of-balloons.cpp
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
// Title: Maximum Number of Balloons
// Description:
// 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.
// Link: https://leetcode.com/problems/maximum-number-of-balloons/
// Time complexity: O(n)
// Space complexity: O(1)
class Solution {
public:
int maxNumberOfBalloons(string text) {
std::string pattern = "balloon";
// store all letters in text into source
std::unordered_map<char,int> source; {
for (char c: text) ++source[c];
}
// store all letters in pattern into target
std::unordered_map<char,int> target; {
for (char c: pattern) ++target[c];
}
// apply max constraint for each letter in target
int result = INT_MAX;
for (auto [c, count]: target) {
int maxVal = source[c] / count;
result = std::min(maxVal, result);
}
return result;
}
};