✅ 1170. 比较字符串最小字母出现频次
描述
我们来定义一个函数 f(s),其中传入参数 s 是一个非空字符串;该函数的功能是统计 s 中(按字典序比较)最小字母的出现频次。
例如,若 s = "dcce",那么 f(s) = 2,因为最小的字母是 "c",它出现了 2 次。
现在,给你两个字符串数组待查表 queries 和词汇表 words,请你返回一个整数数组 answer 作为答案,其中每个 answer[i] 是满足 f(queries[i]) < f(W) 的词的数目,W 是词汇表 words 中的词。
示例 1:
输入:queries = ["cbd"], words = ["zaaaz"]
输出:[1]
解释:查询 f("cbd") = 1,而 f("zaaaz") = 3 所以 f("cbd") < f("zaaaz")。
示例 2:
输入:queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]
输出:[1,2]
解释:第一个查询 f("bbb") < f("aaaa"),第二个查询 f("aaa") 和 f("aaaa") 都 > f("cc")。
提示:
1 <= queries.length <= 2000
1 <= words.length <= 2000
1 <= queries[i].length, words[i].length <= 10
queries[i][j], words[i][j] 都是小写英文字母
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/compare-strings-by-frequency-of-the-smallest-character
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
首先,你要写一个 : 统计单词中最小字母出现数的函数 f
思路是: 对单词进行sort,然后统计和第一个字母相同的个数。
但是对words 使用 f(W) 的时候就出现一个疑惑: 比如我的words:
words = ["a","aa","aaa","aaaa", "bbbbb"]
0 1。 2。 3。 4
//那么 f(words[i]) 里面的 i 应该取 3 还是 4 呢?
// 取3, f = 4, 取4 , f = 5(因为b 有5个)
//是取3 的话,这个f要怎么写呢?也就是还要再比较一次各个字母出现次数对应的字母的大小。then we select someone special in `multi`
》〉》〉实际上,是我理解错了, 如果观看别人的解答, 你现在知道了:我们要对每一个queries[i] 去和
每一个 word 进行比较。it is `one` to `multi`, we dont select anyone special in `multi`
c/java 观看
3 个月前
Java 17ms
public int[] numSmallerByFrequency(String[] queries, String[] words) {
int[] array = new int[words.length];
int[] ans = new int[queries.length];
for (int i = 0; i < array.length; i++)
array[i] = count(words[i]);
for (int i = 0; i < ans.length; i++) {
int count = count(queries[i]);
for (int j = array.length - 1; j >= 0; j--)
ans[i]+=count < array[j]?1:0;
}
return ans;//tt ans[i] meaning: for queries[i] this word, how many
//tt word in words have their `f()` bigger than my `f()` (me aka: queries[i]). if they are bigger, im smaller, ans[i]+1;
}
// 统计最小字母出现数
public static int count(String str) {
int[] alphabet = new int[26];
for (int i = 0; i < str.length(); i++)
alphabet[str.charAt(i) - 'a']++;
for (int count : alphabet)
if (count != 0)
return count;
return 0;
}
---
C
int count(char *str)
{
int i, ch[26] = {0};
for(i = 0;str[i];++i)
++ch[str[i] - 'a'];
for(i = 0;i < 26 && ch[i] == 0;++i);//tt so we find min(which is the first one who is not zero)
return ch[i];
}
int* numSmallerByFrequency(char ** queries, int queriesSize, char ** words, int wordsSize, int* returnSize){
int i, j, cnt;
int *tmp = (int*)calloc(wordsSize, sizeof(int));
int *re = (int*)calloc(queriesSize, sizeof(int));
*returnSize = queriesSize;
for(i = 0;i < wordsSize;++i)
tmp[i] = count(words[i]);
for(i = 0;i < queriesSize;++i)
{
cnt = count(queries[i]);
for(j = 0;j < wordsSize;++j)
{
if(cnt < tmp[j])
re[i] += 1;
}
}
return re;
}
---
py
class Solution:
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
f = lambda x: x.count(min(x))
n, ws = len(words), sorted(map(f, words))
#tt NB todo: what is `bisect` ?? and learn to use `map` !!
return [n - bisect.bisect(ws, i) for i in map(f, queries)]
py
class Solution:
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
def findMinAlphaCount(my_str):#tt: !! you must put your function which you want to use later before the usage:tt3
ch = [0] * 26
for c in my_str:
ch[ord(c) - ord('a')] += 1
for x in ch:
if x != 0:
return x
tmp = [0] * len(words)
ret = [0] * len(queries)
for i, word in enumerate(words):
tmp[i] = findMinAlphaCount(word)#tt3
for j, query in enumerate(queries):
cnt = findMinAlphaCount(query)#tt3: must put function prototype before me!
for cmpare in tmp:
if cnt < cmpare:
ret[j] += 1
return ret
'''
执行用时 :
616 ms
, 在所有 Python3 提交中击败了
35.21%
的用户
内存消耗 :
14.2 MB
, 在所有 Python3 提交中击败了
6.74%
的用户
'''