# Problem Statement You are given two strings `s1` and `s2` of equal length. A **string swap** is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices. Return `true` _if it is possible to make both strings equal by performing **at most one string swap** on **exactly one** of the strings._ Otherwise, return `false`. ## Constraints - `1 <= s1.length, s2.length <= 100` - `s1.length == s2.length` - `s1` and `s2` consist of only lowercase English letters. # Solution To begin with, we define `badIndices` as the list of every index `i` where `s1[i] != s2[i]`. If this list is empty, it means that the two strings are equal, and we return `True`. After we check if it is empty, the only other "good" option requires that the number of bad indices is $2$. If this condition is not met, we return `False`. > [!NOTE] Why? > If the number of bad indices is 1, swapping it will only move the location of the mismatch, and cannot help us. If the number of bad indices is > 2, then we would require more than 1 swap. (or it would be impossible). At this point, we know there are two indices `i` where `s1[i] != s2[i]`. To see if swapping would make the strings equal, we can simply check that the two indices would be correct if swapped. So, at this point we can label the two indices as `i` and `j`, and return the boolean expression `s1[i] == s2[j] and s1[j] == s2[i]`. ```python badIndices = [i for i in range(len(s1)) if s1[i] != s2[i]] if len(badIndices) == 0: return True if len(badIndices) != 2: return False i,j = badIndices return s1[i] == s2[j] and s1[j] == s2[i] ``` Time Complexity: O$(n)$ | Space Complexity: O$(n)$ > [!INFO] Space Complexity Note > We can reduce the space complexity from O$(n)$ to O$(1)$ by using a regular loop instead of a [[list comprehension]] and simply returning `False` if we ever hit a third mismatch. I've chosen to not implement this for the sake of simplicity.