当前位置:网站首页>1189. Maximum number of "balloons"

1189. Maximum number of "balloons"

2022-07-05 00:20:00 Phoenix_ ZengHao

subject

1189.“ balloon ” Maximum number of

The main idea of the topic

Give you a string text, You need to use text To piece together as many words as possible “balloon”( balloon ).

character string text Each letter in can only be used once at most . Please return the maximum number of words you can piece together “balloon”.

Examples

image-20220213232335526

Data scale

image-20220213232343150

Ideas

Considering the composition of words b a l l o o n balloon balloon, There is no need to care about their order . And it is clear that each letter can only be used once , So count the number of each letter directly , Then the number of words that can be formed at last is m i n ( b , a , l / 2 , o / 2 , n ) min(b,a,l/2,o/2,n) min(b,a,l/2,o/2,n).

Code

class Solution {
    
public:
    int vis[30];
    int maxNumberOfBalloons(string text) {
    
        
        for(int i=0;i<text.length();i++){
    
            vis[text[i]-'a']++;
        }
        int ans=min(vis[1],min(vis[0],min(vis['l'-'a']/2,min(vis['o'-'a']/2,vis['n'-'a']))));
        return ans;
    }
};
原网站

版权声明
本文为[Phoenix_ ZengHao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141129574372.html