其他分享
首页 > 其他分享> > leetcode-1160

leetcode-1160

作者:互联网

套路题,难点在于数组处理字母,准确是可能想不到,容易滥用map,map在数据量小的时候真心不推荐使用。

 

func count(word string) []int {
    counter := make([]int, 26)
    for i := 0; i < len(word); i++ {
        c := word[i]
        counter[c - 'a']++
    }
    return counter
}

func contain(chars_counter []int, word_counter []int) bool {
    for i := 0; i < 26; i++ {
        if chars_counter[i] < word_counter[i] {
            return false
        }
    }
    return true
}

func countCharacters(words []string, chars string) int {
    res := 0
    chars_counter := count(chars)
    for _, word := range words {
        word_counter := count(word)
        if contain(chars_counter, word_counter) {
            res += len(word)
        }
    }
    return res
}

 

end

标签:word,string,int,chars,counter,return,1160,leetcode
来源: https://www.cnblogs.com/CherryTab/p/12514652.html