Leetcode 1369 Solution
This article provides solution to leetcode question 1369 (minimum-swaps-to-make-strings-equal)
Access this page by simply typing in "lcs 1369" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/minimum-swaps-to-make-strings-equal
Solution
class Solution:
def minimumSwap(self, s1: str, s2: str) -> int:
cnt1 = 0
cnt2 = 0
for ch1, ch2 in zip(s1, s2):
if ch1 == ch2:
continue
if ch1 == 'x':
cnt1 += 1
else:
cnt2 += 1
if (cnt1 + cnt2) % 2 == 1:
return -1
if cnt1 == 0 and cnt2 == 0:
return 0
elif cnt1 == 1 and cnt2 == 1:
return 2
elif cnt1 % 2 == 0 and cnt2 % 2 == 0:
return (cnt1 + cnt2) // 2
else:
return 1 + (cnt1 + cnt2) // 2