signed

QiShunwang

“诚信为本、客户至上”

LeetCode题解(1790):仅执行一次字符串交换能否使两个字符串相等(Python)

2021/5/14 21:47:05   来源:

题目:原题链接(简单)

标签:字符串

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( 1 ) O(1) O(1)40ms (64.61%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def areAlmostEqual(self, s1: str, s2: str) -> bool:
        size = len(s1)
        diff = -1
        already = False
        for i in range(size):
            if s1[i] != s2[i]:
                if already:
                    return False
                if diff == -1:
                    diff = i
                else:
                    if not (s1[diff] == s2[i] and s2[diff] == s1[i]):
                        return False
                    else:
                        already = True
        return diff == -1 or already is True