【LeetCode】859. 亲密字符串

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time: 2019/3/13
# @Author: xfLi
# The file...

def buddyStrings(A, B):
    if len(A) != len(B): return False
    if A == B:   # 相等情况
        seen = set()
        for a in A:
            if a in seen: return True
            seen.add(a)
        return False
    else:  # A,B不同的情况
        temp = []
        for a, b in zip(A, B):
            if a != b:
                temp.append([a,b])
        if len(temp) > 2: return False   # 超过2处不同
        return len(temp) == 2 and  temp[0] == temp[1][::-1]

if __name__ == '__main__':
    A = 'aab'
    B = 'aab'
    result = buddyStrings(A, B)
    print(result)

猜你喜欢

转载自blog.csdn.net/qq_30159015/article/details/88537571
今日推荐