luoheng
10/11/2019 - 8:01 AM

numSpecialEquivGroups


func isEqual(str1, str2 string) bool {
    arr1Odd, arr2Odd := [26]int{}, [26]int{}
    arr1Even, arr2Even := [26]int{}, [26]int{}
    for i := 0; i < len(str1); i++ {
        if i % 2 == 0 {
            arr1Odd[str1[i]-'a']++
            arr2Odd[str2[i]-'a']++
        } else {
            arr1Even[str1[i]-'a']++
            arr2Even[str2[i]-'a']++
        }
    }
    return arr1Odd == arr2Odd && arr1Even == arr2Even
}

func numSpecialEquivGroups(A []string) int {
    counted := map[int]bool{}
    sum := 0
    for i, a := range A {
        if !counted[i] {
            sum++
            counted[i] = true
            for j := i+1; j < len(A); j++ {
                if !counted[j] && isEqual(a, A[j]) {
                    counted[j] = true
                }
            }
        }
    }
    return sum
}