[ Solution]1189. Maximum Number of Balloons

·

1 min read

Problem

Problem_Link

Solutions (time, space)

O(n), O(1)

class Solution {
    public static int maxNumberOfBalloons(String text) {
        /*
        //for all alphabet characters
        int[] charList = new int[26];
        for (char c : text.toCharArray()) {
            charList[c - 'a']++;
        }
        */
        int b = 0, a = 0, l = 0, o = 0, n = 0;

        for (int temp : text.toCharArray()) {
            switch (temp) {
                case 'b':
                    ++b;
                    break;
                case 'a':
                    ++a;
                    break;
                case 'l':
                    ++l;
                    break;
                case 'o':
                    ++o;
                    break;
                case 'n':
                    ++n;
                    break;
                default:
                    break;
            }
        }

        return IntStream.of(b, a, l / 2, o / 2, n).min().getAsInt();
    }
}

Explanation

  • used int instead of array because there are only 5 characters need to check.
  • IntStream is possible to use if over JAVA 8
  • IntStream is basically same as using Math or logic to find min.