1170. Compare Strings by Frequency of the Smallest Character

读懂题就不难了,直接在queries里面存f 处理过的函数,可以将string转换成UTF code,用Math.min 找到最小值出现了几次,之后直接二分就行。
代码的注意点:
const min = Math.min(...arr) 这里要用spread展开
item.charCodeAt(0) 转换成 UTF-16 code
ts
function numSmallerByFrequency(queries: string[], words: string[]): number[] {
const f = (arr: number[]): number => {
const min = Math.min(...arr)
return arr.filter((item) => {
if (item === min) {
return true
}
}).length
}
const binarySearch = (left, right, target, arr) => {
while (left < right) {
const mid = Math.floor((left + right) / 2)
if (arr[mid] < target) {
left = mid + 1
} else {
right = mid
}
}
return left
}
const ans = []
const newQueries = []
const newWords = []
queries.forEach((item) => {
const strArray = item.split('')
newQueries.push(f(strArray.map((item) => item.charCodeAt(0))))
})
words.forEach((item) => {
const strArray = item.split('')
newWords.push(f(strArray.map((item) => item.charCodeAt(0))))
})
newWords.sort((a, b) => a - b)
for (const i of newQueries) {
ans.push(newWords.length - binarySearch(0, newWords.length, i + 1, newWords))
}
return ans
}