1189. “气球” 的最大数量

Maximum Number of Balloons

题目描述:

Given a string text, you want to use the characters of text to form as many instances of the word “balloon” as possible.

You can use each character in text at most once. Return the maximum number of instances that can be formed.

Example 1:

Input: text = “nlaebolko”
Output: 1
Example 2:

Input: text = “loonbalxballpoon”
Output: 2
Example 3:

Input: text = “leetcode”
Output: 0

思路:

找这些词的频率,找到最短板的单词即可

代码:

func maxNumberOfBalloons(text string) int {
    s := make([]int, 5)
    for i :=0 ;i<len(text); i++{
        if text[i] == 'b'{
            s[0]++
        }else if text[i] == 'a'{
            s[1]++
        }else if text[i] == 'l'{
            s[2]++
        }else if text[i] == 'o'{
            s[3]++
        }else if text[i] == 'n'{
            s[4]++
        }
    } 
    s[2] /= 2
    s[3] /= 2
    sort.Ints(s)
    return s[0]
}

代码效率:

执行用时:0 ms, 在所有 Go 提交中击败了100.00%的用户
内存消耗:2.2 MB, 在所有 Go 提交中击败了100.00%的用户


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!