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

1189. Maximum number of "balloons"

2022-07-06 06:57:00 _ Alkaid_

difficulty : Simple

Catalog

One 、 Problem description

Two 、 Ideas

3、 ... and 、 Problem solving

1、 Code implementation

2、 Time complexity and Spatial complexity


One 、 Problem description

I'm going to use LeetCode The description above .

         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".

Here is an example :

Tips :

  • 1 <= text.length <= 10^4
  • text  All consist of lowercase English letters

Two 、 Ideas

        Need to use the given text Put together as many pieces as possible "balloon" that , We need statistics text in Existing characters :' b ', ' a ', ' l ', ' o '' n ' The number of . And then calculate , The current character ' b ', ' a ', ' l ', ' o '' n ' How many can be formed by the number of "balloon" . Because here "balloon" in ' l ' And ' o ' The number of Are the two , The number of other characters is one , So the statistics are finished After the number of characters . The character ' l ' And ' o ' The number of Divide by two , Retake  ' b ', ' a ', ' l ', ' o '' n ' The number of minimum value , That is, it can form "balloon" The number of .

3、 ... and 、 Problem solving

1、 Code implementation

class Solution {
public:
    int maxNumberOfBalloons(string text) {
        unordered_map<char,int> hashTable;
        for(auto& item : text){
            if(item == 'b' or
            item == 'a' or
            item == 'l' or
            item == 'o' or
            item == 'n'){
                hashTable[item]++;
            }
        }
        hashTable['l'] /= 2;
        hashTable['o'] /= 2;
        int ans = min(hashTable['b'],min(hashTable['a'],min(hashTable['l'],min(hashTable['o'],hashTable['n']))));
        return ans;
    }
};

2、 Time complexity and Spatial complexity

Time complexity :O(n),n by text Size .

Spatial complexity :O(n)

原网站

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