Skip to content

1189. Maximum Number of Balloons 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  int maxNumberOfBalloons(string text) {
    int ans = INT_MAX;
    vector<int> count(26);

    for (char c : text)
      ++count[c - 'a'];

    for (char c : string("ban"))
      ans = min(ans, count[c - 'a']);

    for (char c : string("lo"))
      ans = min(ans, count[c - 'a'] / 2);

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
  public int maxNumberOfBalloons(String text) {
    int ans = Integer.MAX_VALUE;
    int[] count = new int[26];

    for (char c : text.toCharArray())
      ++count[c - 'a'];

    for (char c : new char[] {'b', 'a', 'n'})
      ans = Math.min(ans, count[c - 'a']);

    for (char c : new char[] {'o', 'l'})
      ans = Math.min(ans, count[c - 'a'] / 2);

    return ans;
  }
}
1
2
3
4
class Solution:
  def maxNumberOfBalloons(self, text: str) -> int:
    count = collections.Counter(text)
    return min(count['b'], count['a'], count['l'] // 2, count['o'] // 2, count['n'])