给出一个0-1字符串,问最少操作几次可以将字符串变为....0-1-0-1....用两个计数器记录首位取0和取1分别要操作几次,取两者较小值
#1758
#Runtime: 25 ms (Beats 79.41%)
#Memory: 13.4 MB (Beats 88.24)
class Solution(object):
def minOperations(self, s):
"""
:type s: str
:rtype: int
"""
ans1, ans2 = 0, 0
for i in xrange(0, len(s)):
if i % 2:
if s[i] == '0':
ans1 += 1
else:
ans2 += 1
else:
if s[i] == '1':
ans1 += 1
else:
ans2 += 1
return min(ans1, ans2)