5247. 交换字符使得字符串相同

比赛时写的,比较傻

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

你可能感兴趣的:(Leetcode,算法)