Leetcode Note: Go - Rings and Rods

Rings and Rods - LeetCode
https://leetcode.com/problems/rings-and-rods/

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

回答

Golang 0ms, and 100% memory 1,9 and easy you understand - Rings and Rods - LeetCode
https://leetcode.com/problems/rings-and-rods/solutions/2997555/golang-0ms-and-100-memory-1-9-and-easy-you-understand/

//example 1, 0ms 100% memory
func countPoints(rings string) int {
    rod := make(map[string]string)
    
    for i := 0; i < len(rings); i += 2{
        s1 := string(rings[i])
        s2 := string(rings[i+1])
        rod[s2] += s1
    }

    count := 0
    for _, v := range rod {
        if Unique(v) == "BGR" {
            count++
        }
    }

    return count;
}

func Unique(s string) string{
    arr := make([]int, 26)

    str := ""
    for _, n := range s { arr[n - 'A'] = 1}

    for i, n := range arr{
        if n != 0 {
            str += string(i + 'A')
        }
    }
    
    return str
}