Leetcode Note: Go - Minimum Changes to Make Alternating Binary String

Minimum Changes To Make Alternating Binary String - LeetCode
https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/

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

回答

Simple Go solution - Minimum Changes To Make Alternating Binary String - LeetCode
https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/solutions/2456862/simple-go-solution/

func minOperations(s string) int {
    first, second := 0, 0
    
    for i := 0; i < len(s); i++ {
        if i%2 == 0 {
            if s[i] != '0' {
                first++
            } else {
                second++
            }
        } else {
            if s[i] != '1' {
                first++
            } else {
                second++
            }
        }
    }
    
    return min(len(s)-first, len(s)-second)
}

func min(a, b int) int {
    if a < b {
        return a
    }
    
    return b
}