Skip to content
LC-1790 Easy LeetCode

1790. Check if One String Swap Can Make Strings Equal

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 49% Topics: Hash Table, String, Counting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

import itertools


class Solution(object):
    def areAlmostEqual(self, s1, s2):
        """
        :type s1: str
        :type s2: str
        :rtype: bool
        """
        diff = []
        for a, b in itertools.izip(s1, s2):
            if a == b:
                continue
            if len(diff) == 2:
                return False
            diff.append([a, b] if not diff else [b, a])
        return not diff or (len(diff) == 2 and diff[0] == diff[1])

Solution from kamyu104/LeetCode-Solutions · MIT