luoheng
10/12/2019 - 10:21 AM

shortestCompletingWord


func count(word string) [26]int {
    count := [26]int{}
    for i := 0; i < len(word); i++ {
        if word[i] >= 'a' && word[i] <= 'z' {
            count[word[i]-'a']++
        } else if word[i] >= 'A' && word[i] <= 'Z' {
            count[word[i]-'A']++
        }
    }
    return count
}

func shortestCompletingWord(licensePlate string, words []string) string {
    cl := count(licensePlate)
    res, minL := "", 15
    for _, word := range words {
        cw, i := count(word), 0
        for i = 0; i < len(cw); i++ {
            if cw[i] < cl[i] {
                break
            }
        }
        if i == len(cw) && len(word) < minL {
            res, minL = word, len(word)
        }
    }
    return res
}