Leetcode Note: Go - Substrings of Size Three With Distinct Characters

Substrings of Size Three with Distinct Characters - LeetCode
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/

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

回答

Go solution - Substrings of Size Three with Distinct Characters - LeetCode
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/solutions/2464237/go-solution/

func countGoodSubstrings(s string) int {
    charSet, count, left := make([]byte, 26), 0, 0
    
    for right := 0; right < len(s); right++ {
        charSet[s[right] - 'a']++
        
        for right - left + 1 > 3 {
            charSet[s[left] - 'a']--
            left++
        }
        
        if right-left+1 == 3 {
            charCount := 0
            
            for i := 0; i < 26; i++ {
                if charSet[i] > 0 {
                    charCount++
                }    
            }
            
            if charCount == 3 {
                count++
            }
        }
    }
    
    return count
}