luoheng
10/7/2019 - 7:44 AM

countCharacters

func countCharacters(words []string, chars string) int {
    cHash := [26]int{}
    for _, c := range chars {
        cHash[c-'a']++
    }
    sum := 0
    bHash := [26]int{}
    for _, str := range words {
        for _, c := range str {
            bHash[c-'a']++
        }
        for i := 0; i < 26; i++ {
            if bHash[i] > cHash[i] {
                sum -= len(str)
                break
            }
        }
        bHash = [26]int{}
        sum += len(str)
    }
    return sum
}