Leetcode Note: Go - Find Words That Can Be Formed by Characters

Find Words That Can Be Formed by Characters - LeetCode
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/

  • Go 言語で取り組んだメモ

回答

Simple solution with Go - LeetCode Discuss
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2476433/Simple-solution-with-Go

func countCharacters(words []string, chars string) int {
    charSet := make([]int, 26)
    
    for i := 0; i < len(chars); i++ {
        charSet[int(chars[i] - 'a')]++
    }
    
    sum := 0
    
    for _, word := range words {
        currentCharSet := make([]int, 26)
        copy(currentCharSet, charSet)
        
        isValid := true
        
        for i := 0; i < len(word); i++ {
            currentCharSet[int(word[i] - 'a')]--
            
            if currentCharSet[int(word[i] - 'a')] < 0 {
                isValid = false
                break
            }
        }
        
        if isValid {
            sum += len(word)
        }
    }
    
    return sum
}