You are given two strings s1 and s2 of equal length consisting of letters "x" and "y" only. Your task is to make these two strings equal to each other. You can swap any two characters that belong to different strings, which means: swap s1[i] and s2[j].
Return the minimum number of swaps required to make s1 and s2 equal, or return -1 if it is impossible to do so.
 
Example 1:
Input: s1 = "xx", s2 = "yy"
Output: 1
Explanation: 
Swap s1[0] and s2[1], s1 = "yx", s2 = "yx".
Example 2: 
Input: s1 = "xy", s2 = "yx"
Output: 2
Explanation: 
Swap s1[0] and s2[0], s1 = "yy", s2 = "xx".
Swap s1[0] and s2[1], s1 = "xy", s2 = "xy".
Note that you can't swap s1[0] and s1[1] to make s1 equal to "yx", cause we can only swap chars in different strings.
Example 3:
Input: s1 = "xx", s2 = "xy"
Output: -1

Example 4:
Input: s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
Output: 4

 
Constraints:

1 <= s1.length, s2.length <= 1000
s1, s2 only contain 'x' or 'y'.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Mathematics

Greedy algorithms

Starter code: class Solution:
    def minimumSwap(self, s1: str, s2: str) -> int:
        

Solutions:
class Solution:

	def minimumSwap(self, s1: str, s2: str) -> int:
		xy_pair = 0
		yx_pair = 0
		for (c1, c2) in zip(s1, s2):
			if c1 == 'x' and c2 == 'y':
				xy_pair += 1
			elif c1 == 'y' and c2 == 'x':
				yx_pair += 1
		if (xy_pair + yx_pair) % 2 == 1:
			return -1
		return xy_pair // 2 + yx_pair // 2 + xy_pair % 2 + yx_pair % 2


class Solution:

	def minimumSwap(self, s1: str, s2: str) -> int:
		h = defaultdict(int)
		count = 0
		for i in range(len(s1)):
			if s1[i] != s2[i]:
				count += 1
				h[s1[i]] += 1
		if count % 2 != 0:
			return -1
		(res, a, b) = (0, h['x'], h['y'])
		res += a // 2 + b // 2
		if a % 2 == 0:
			return res
		return res + 2


class Solution:

	def minimumSwap(self, s1: str, s2: str) -> int:
		(x_y, y_x) = (0, 0)
		for (i, j) in zip(s1, s2):
			if i != j:
				if i == 'x':
					x_y += 1
				else:
					y_x += 1
		if (x_y + y_x) % 2 == 1:
			return -1
		res = 0
		res += x_y // 2
		res += y_x // 2
		if x_y % 2 == 1:
			res += 2
		return res


class Solution:

	def minimumSwap(self, s1: str, s2: str) -> int:
		(cntxy, cntyx, n) = (0, 0, len(s1))
		for i in range(n):
			if s1[i] == 'x' and s2[i] == 'y':
				cntxy += 1
			elif s1[i] == 'y' and s2[i] == 'x':
				cntyx += 1
		if (cntxy + cntyx) % 2 == 1:
			return -1
		if cntxy % 2 == 1:
			return cntxy // 2 + cntyx // 2 + 2
		else:
			return cntxy // 2 + cntyx // 2


class Solution:

	def minimumSwap(self, s1: str, s2: str) -> int:
		xx = []
		for i in range(len(s1)):
			if s1[i] != s2[i]:
				xx.append(s1[i])
		if len(xx) % 2 == 1:
			return -1
		res = len(xx) // 2
		if xx.count('x') % 2 == 1:
			res += 1
		return res


class Solution:

	def minimumSwap(self, s1: str, s2: str) -> int:
		x = 0
		y = 0
		for index in range(0, len(s1)):
			if s1[index] != s2[index]:
				if s1[index] == 'x':
					x += 1
				else:
					y += 1
		mid = (x + y) / 2
		(x, y) = (x % 2, y % 2)
		if x + y == 1:
			return -1
		if x + y == 2:
			mid += 1
		print(mid)
		return int(mid)


class Solution:

	def minimumSwap(self, s1: str, s2: str) -> int:
		x_y = y_x = 0
		res = 0
		for i in range(len(s1)):
			if s1[i] != s2[i]:
				if s1[i] == 'x':
					x_y += 1
				else:
					y_x += 1
		if (x_y + y_x) % 2 == 1:
			return -1
		res = x_y // 2
		res += y_x // 2
		if x_y % 2 == 1:
			res += 2
		return res


class Solution:

	def minimumSwap(self, s1: str, s2: str) -> int:
		xy = 0
		yx = 0
		for (m, n) in zip(s1, s2):
			if m == 'x' and n == 'y':
				xy += 1
			elif m == 'y' and n == 'x':
				yx += 1
		if (xy + yx) % 2 != 0:
			return -1
		else:
			return xy // 2 + yx // 2 + xy % 2 + yx % 2


class Solution:

	def minimumSwap(self, s1: str, s2: str) -> int:
		a = 0
		b = 0
		for i in range(len(s1)):
			if s1[i] == 'x' and s2[i] == 'y':
				a += 1
			elif s1[i] == 'y' and s2[i] == 'x':
				b += 1
		if a % 2 == 0 and b % 2 == 0:
			return a // 2 + b // 2
		elif a % 2 == 1 and b % 2 == 1:
			return a // 2 + b // 2 + 2
		else:
			return -1