forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1189. Maximum Number of Balloons.cpp
More file actions
89 lines (51 loc) · 1.37 KB
/
1189. Maximum Number of Balloons.cpp
File metadata and controls
89 lines (51 loc) · 1.37 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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.
Example 1:
Input: text = "nlaebolko"
Output: 1
Example 2:
Input: text = "loonbalxballpoon"
Output: 2
Example 3:
Input: text = "leetcode"
Output: 0
Constraints:
1 <= text.length <= 10^4
text consists of lower case English letters only.
class Solution {
public:
int maxNumberOfBalloons(string text) {
if(text.length()==0) return 0;
unordered_map<char, int> mp;
for(auto ch: text)
mp[ch]++;
string tmp="balloon";
int n=tmp.length();
int res=0;
while(1){
for(int i=0;i<n;i++){
if(mp[tmp[i]]>0){
mp[tmp[i]]--;
} else return res;
}
res++;
}
return res;
}
};
class Solution {
public:
int maxNumberOfBalloons(string text) {
if(text.length()==0) return 0;
unordered_map<char, int> mp;
for(auto ch: text)
mp[ch]++;
int res=INT_MAX;
res=min(res,mp['b']);
res=min(res,mp['a']);
res=min(res,mp['l']/2);
res=min(res,mp['o']/2);
res=min(res,mp['n']);
return res;
}
};