Minimum Number of Swaps to Make the Binary String Alternating LeetCode solution in Python
1class Solution:2def minSwaps(self, st: str) -> int:34def swap(st, c):5n = len(st)6mis = 07for i in range(n):8if i % 2 == 0 and st[i] != c:9mis += 110if i % 2 == 1 and st[i] == c:11mis += 112return mis // 21314dic = Counter(st)15z = dic["0"]16o = dic["1"]17res = 018if abs(z - o) > 1:19return -120elif z > o:21res = swap(st, "0")22elif o > z:23res = swap(st, "1")24else:25res = min(swap(st, "0"), swap(st, "1"))2627return res