You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First we count how many consecutive 0s and 1s we have and store them in groups
groups= []
count=1foriinrange(1, len(s)):
ifs[i] ==s[i-1]:
count+=1else:
groups.append(count)
count=1groups.append(count) # Add the last group# print(groups) => [2, 2, 2, 2] two 0, two 1, two 0, two 1
Then we take 2 in 2 groups and add the answer to the smallest of the stored numbers
ans=0foriinrange(1, len(groups)):
ans+=min(groups[i-1], groups[i]) # Form pairs with groups