Leetcode 1170. 比较字符串最小字母出现频次
1. 问题描述
2. 思路
2.1 思路1
暴力
3. 代码
3.1 思路1代码
func numSmallerByFrequency(queries []string, words []string) []int {
var res []int
counterQ := getCounter(queries)
counterW := getCounter(words)
for i := 0; i < len(queries); i++ {
counter := 0
for j := 0; j < len(words); j++ {
if counterW[j] > counterQ[i] {
counter++
}
}
res = append(res, counter)
}
return res
}
func getCounter(words []string) []int {
var res []int
for i := 0; i < len(words); i++ {
counter := 0
wordByte := []byte(words[i])
letter := getMinLetter(wordByte)
for j := 0; j < len(wordByte); j++ {
if letter == wordByte[j] {
counter++
}
}
res = append(res, counter)
}
return res
}
func getMinLetter(arr []byte) byte {
min := arr[0]
for i := 0; i < len(arr); i++ {
if arr[i] < min {
min = arr[i]
}
}
return min
}