Maximum Number of Balloons Problem & Solution

See the maximum number of balloons problem on LeetCode.

C++ Solution

#pragma GCC optimize(“Ofast”)
#pragma GCC optimizations(“unroll-loops”)

static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
public:
  int maxNumberOfBalloons(string text) {
    int best = numeric_limits<int>::max();
    int freq[‘z’ - ‘a’ + 1] = {};
    
    for (int i = 0; i < text.size(); ++i) {
      ++freq[text[i] - ‘a’];
    }
    
    best = min({
      freq[‘b’ - ‘a’],
      freq[‘a’ - ‘a’],
      freq[‘l’ - ‘a’] / 2,
      freq[‘o’ - ‘a’] / 2,
      freq[‘n’ - ‘a’],
    });
    
    return best;
  }
};

Sample Search Queries

Many Paths. Follow Yours.