LeetCode.1189-balloon maximum number of instances (Maximum Number of Balloons)

This is the first Ogawa 416 update, the first 449 Pian original


Look problems and prepare

Introduced today is LeetCode algorithm in title Easy level of 267 questions (overall title number is 1189 ). Given a string of text, use text characters to form words "balloon"as much as possible instance. Each character can be used once in the text the most. It returns the maximum number of instances that may be formed.

E.g:

Input: text = "nlaebolko"
Output: 1

Input: text = "loonbalxballpoon"
Output: 2

Input: text = "leetcode"
Output: 0

Constraints :

  • 1 <= text.length <= 10^4
  • Text string contains only lowercase letters.

The first solution

Meant to subject a given string, the string is composed can be found "balloon"the largest number of characters, the nature and volume of water in cask determined by similar shortcomings.

Direct traversal text string of characters, pairs of letters a, b, l, n, othe number of occurrences counted as two l and o are required, after counting, the need for land othe number of divided by 2, and then compare the number of occurrences five letters minimum, because only the minimum number of letters that appear will ultimately determine the composition of how many "balloon".

public int maxNumberOfBalloons(String text) {
    if (text == "" || text.length() < 7) {
        return 0;
    }
    int A = 0, B = 0, L = 0, O = 0, N = 0;
    int len = text.length();
    for (int i=0; i<len; i++) {
        if (text.charAt(i) == 'a') {
            A++; 
        } else if (text.charAt(i) == 'b') {
            B++;
        } else if (text.charAt(i) == 'l') {
            L++;
        } else if (text.charAt(i) == 'n') {
            N++;
        } else if (text.charAt(i) == 'o') {
            O++;
        }
    }
    L /= 2;
    O /= 2;
    int min = Math.min(A, B);
    min = Math.min(min, N);
    if (min == 0) {
        return 0;
    }
    if (L !=0 && O != 0) {
        min = Math.min(min, L);
        min = Math.min(min, O);
        return min;
    }
    return 0;
}


The second solution

For the first solution, the letter is determined in a loop manner, can be replaced with a length of 26 using the intarray, according to the order of 26 letters of the alphabet, the array values can be directly, and returns the number of 5 minimum.

public int maxNumberOfBalloons2(String text) {
    if (text == "" || text.length() < 7) {
        return 0;
    }
    int A = 0, B = 0, L = 0, O = 0, N = 0;
    int len = text.length();
    int[] arr = new int[26];
    for (int i=0; i<len; i++) {
        arr[text.charAt(i)-'a']++;
    }
    A = arr[0];
    B = arr[1];
    L = arr[11]/2;
    N = arr[13];
    O = arr[14]/2;
    int min = Math.min(A, B);
    min = Math.min(min, N);
    min = Math.min(min, L);
    min = Math.min(min, O);
    return min;
}


A third solution

For the second solution, we can five local variables also omitted, after all, just compare the minimum, go directly to the array take on the line.

public int maxNumberOfBalloons3(String text) {
    if (text == "" || text.length() < 7) {
        return 0;
    }
    int len = text.length();
    int[] arr = new int[26];
    for (int i=0; i<len; i++) {
        arr[text.charAt(i)-'a']++;
    }
    int min = Math.min(arr[0], arr[1]); //a b
    min = Math.min(min, arr[13]); // n
    min = Math.min(min, arr[11]/2); // l
    min = Math.min(min, arr[14]/2); // o
    return min;
}


summary

Thematic algorithm has now updated LeetCode algorithm of feature articles 273 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures either] in a keyword, to obtain a series of articles collection.

That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, leave a message, it is the greatest reward in watching and supporting me!

Guess you like

Origin www.cnblogs.com/xiaochuan94/p/11595591.html