当前位置:网站首页>Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)

Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)

2022-06-26 16:47:00 I'm not xiaohaiwa~~~~

 Insert picture description here
Define a function f(s), Statistics s in ( Compare in dictionary order ) The frequency of the smallest letter , among s Is a non empty string .

for example , if s = “dcce”, that f(s) = 2, Because the smallest letter in the dictionary order is “c”, It appears 2 Time .

Now? , Here are two string arrays to look up queries And vocabulary words . For each query queries[i] , Statistics required words Meet in f(queries[i]) < f(W) Of The number of words ,W Represents a vocabulary words Every word in .

Please return an integer array answer As the answer , Each of them answer[i] It's No i Results of this query .

Example 1:

 Input :queries = ["cbd"], words = ["zaaaz"]
 Output :[1]
 explain : Inquire about  f("cbd") = 1, and  f("zaaaz") = 3  therefore  f("cbd") < f("zaaaz").

Example 2:

 Input :queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]
 Output :[1,2]
 explain : First query  f("bbb") < f("aaaa"), The second query  f("aaa")  and  f("aaaa")  all  > f("cc").
 

Tips :

  • 1 <= queries.length <= 2000
  • 1 <= words.length <= 2000
  • 1 <= queries[i].length, words[i].length <= 10
  • queries[i][j]、words[i][j] It's all made up of lowercase letters

Code:

class Solution {
    
public:
    vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
    
        vector<int>vecword;
        for(int i=0;i<words.size();i++)
        {
    
            string temp=words[i]; 
            sort(temp.begin(),temp.end());
            vecword.push_back(count(temp.begin(),temp.end(),temp[0]));
        }
        sort(vecword.begin(),vecword.end());
        vector<int>res;
        
        for(int i=0;i<queries.size();i++)
        {
    
            string temp=queries[i];
            sort(temp.begin(),temp.end());
            int cnt=count(temp.begin(),temp.end(),temp[0]);
            int ans=0;
            ans=count_if(vecword.begin(),vecword.end(),[=](int a){
    return a>cnt;});
            
            res.push_back(ans);
            
        }
        return res;
    }
};
原网站

版权声明
本文为[I'm not xiaohaiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261636026665.html