Leetcode Note: Go - Check if All Characters Have Equal Number of Occurrences

Check if All Characters Have Equal Number of Occurrences - LeetCode
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/

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

回答

Go | Time: O(n) / 0 ms / 100% | Space: O(1) / 2.3 MB / 89.74% - Check if All Characters Have Equal Number of Occurrences - LeetCode
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/solutions/1545908/go-time-o-n-0-ms-100-space-o-1-2-3-mb-89-74/

func areOccurrencesEqual(s string) bool {
    count, prev := make(map[rune]int), 0
    
    for _, l := range s {
        count[l]++
    }
    
    for _, v := range count {
        switch {
        case prev <= 0:
            prev = v
        case prev != v:
            return false
        }
    }

    return true
}