luoheng
10/12/2019 - 6:32 AM

numSmallerByFrequency


func f(word string) int {
    cur, sum := word[0], 1
    for i := 1; i < len(word); i++ {
        if word[i] == cur {
            sum++
        } else if word[i] < cur {
            cur, sum = word[i], 1
        }
    }
    return sum
}

func numSmallerByFrequency(queries []string, words []string) []int {
    count := make([]int, 12)
    for _, word := range words {
        count[f(word)]++
    }
    for i := len(count)-2; i >= 0; i-- {
        count[i] += count[i+1]
    }
    res := make([]int, len(queries))
    for i, word := range queries {
        res[i] = count[f(word)+1]
    }
    return res
}