Участник:Plague rat/Minimum Swaps to Make Strings Equal

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/

class Solution:
    def minimumSwap(self, s1: str, s2: str) -> int:
        diffX = 0
        diffY = 0
        for i, _char in enumerate(s1):
            if s1[i] != s2[i] and _char == 'x':
                diffX += 1
            elif s1[i] != s2[i] and _char == 'y':
                diffY += 1
        if (diffY + diffX) % 2 != 0:
            return -1
        if diffX % 2 == 0:
            return (diffX + diffY) // 2
        else:
            return (diffX + diffY) // 2 + 1