Skip to content

Latest commit

 

History

History
29 lines (27 loc) · 713 Bytes

1758.md

File metadata and controls

29 lines (27 loc) · 713 Bytes

1758. Minimum Changes To Make Alternating Binary String

Solution 1 (time O(n), space O(1))

class Solution(object):
    def minOperations(self, s):
        """
        :type s: str
        :rtype: int
        """
        ans1 = 0
        t = s[0]
        for i in range(1, len(s)):
            if s[i] == t:
                ans1 += 1
                t = str(1 - int(s[i]))
            else:
                t = s[i]
        ans2 = 1
        t = str(1 - int(s[0]))
        for i in range(1, len(s)):
            if s[i] == t:
                ans2 += 1
                t = str(1 - int(s[i]))
            else:
                t = s[i]
        return min(ans1, ans2)