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
.
Example 1:
Input: s1 = "bank", s2 = "kanb" Output: true Explanation: For example, swap the first character with the last character of s2 to make "bank".
Example 2:
Input: s1 = "attack", s2 = "defend" Output: false Explanation: It is impossible to make them equal with one string swap.
Example 3:
Input: s1 = "kelb", s2 = "kelb" Output: true Explanation: The two strings are already equal, so no string swap operation is required.
Constraints:
1 <= s1.length, s2.length <= 100
s1.length == s2.length
s1
and s2
consist of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to see if we can make two strings identical by swapping just two letters in one of them. The brute force method involves trying every possible pair of letters to swap and then checking if the strings become equal.
Here's how the algorithm would work step-by-step:
def can_make_equal_with_one_swap(first_string, second_string):
string_length = len(first_string)
for first_index_to_swap in range(string_length):
for second_index_to_swap in range(first_index_to_swap + 1, string_length):
# Create a list so that we can perform the swap
first_string_as_list = list(first_string)
# Perform the swap
first_string_as_list[first_index_to_swap], first_string_as_list[second_index_to_swap] = \
first_string_as_list[second_index_to_swap], first_string_as_list[first_index_to_swap]
# Check if the strings are equal
if "".join(first_string_as_list) == second_string:
return True
# This puts the first_string back into its original configuration
first_string_as_list[first_index_to_swap], first_string_as_list[second_index_to_swap] = \
first_string_as_list[second_index_to_swap], first_string_as_list[first_index_to_swap]
# If no swaps resulted in equality return false
return False
The goal is to see if swapping just two letters in one of the strings can make them identical. We'll pinpoint the differences between the strings and then check if a single swap can fix those differences.
Here's how the algorithm would work step-by-step:
def strings_can_be_made_equal_by_one_swap(string1, string2):
string_length_1 = len(string1)
string_length_2 = len(string2)
if string_length_1 != string_length_2:
return False
difference_indices = []
for index in range(string_length_1):
if string1[index] != string2[index]:
difference_indices.append(index)
# Strings are equal, check lengths before returning.
if not difference_indices:
return True
# More than two differences means one swap can't fix it.
if len(difference_indices) != 2:
return False
index1, index2 = difference_indices
# Verify if the swap makes strings equal.
if string1[index1] == string2[index2] and string1[index2] == string2[index1]:
return True
return False
Case | How to Handle |
---|---|
Null or empty input strings | Return true if both are null/empty, false if one is null and the other isn't; consider empty strings as equal. |
Strings of different lengths | Return false immediately, as a single swap cannot make strings of different lengths equal. |
Strings are identical | Return true immediately, as zero swaps are considered valid and no swap is needed. |
Strings differ by more than two characters | Return false immediately, as a single swap can only correct two mismatched characters. |
Strings differ by exactly one character | Return false immediately, as a single swap requires two differing characters. |
Strings have same characters but in different order (other than one swap) | A single swap won't fix this, so after finding two differences, perform the swap and if the strings don't match after the swap, return false. |
Strings contain non-alphanumeric characters | The solution should work regardless of the character set as long as comparison operations are valid. |
Strings are very long (performance) | The solution has linear time complexity O(n), which scales well with long strings. |