1189. Maximum Number of Balloons
Previous1170. Compare Strings by Frequency of the Smallest CharacterNext1268. Search Suggestions System
Last updated
Last updated
Input: text = "nlaebolko"
Output: 1Input: text = "loonbalxballpoon"
Output: 2Input: text = "leetcode"
Output: 0int maxNumberOfBalloons(string text) { // time: O(n + m); space: O(1)
const string target = "balloon";
vector<int> record(26, 0), target_cnt(26, 0);
for (char ch : target)
++target_cnt[ch - 'a'];
for (char ch : text)
++record[ch - 'a'];
int res = 0, n = text.length(), m = target.length();
while (n >= m) {
for (int i = 0; i < 26; ++i) {
if (record[i] < target_cnt[i]) break;
record[i] -= target_cnt[i];
if (i == 25) ++res;
}
n -= m;
}
return res;
}