Given two strings s
and goal
, return true
if you can swap two letters in s
so the result is equal to goal
, otherwise, return false
.
Swapping letters is defined as taking two indices i
and j
(0-indexed) such that i != j
and swapping the characters at s[i]
and s[j]
.
0
and 2
in "abcd"
results in "cbad"
.Example 1:
Input: s = "ab", goal = "ba" Output: true Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.
Example 2:
Input: s = "ab", goal = "ab" Output: false Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.
Example 3:
Input: s = "aa", goal = "aa" Output: true Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.
Constraints:
1 <= s.length, goal.length <= 2 * 104
s
and goal
consist of lowercase 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 basic idea is to explore every single potential swap of two letters within the first word and see if it makes the first word identical to the second word. We will examine each possible swap and check if that swap leads to a solution.
Here's how the algorithm would work step-by-step:
def buddy_strings_brute_force(first_word, second_word):
word_length = len(first_word)
if word_length != len(second_word):
return False
# Iterate through all possible pairs of indices
for first_index in range(word_length):
for second_index in range(first_index + 1, word_length):
# Create a list to modify the first word
modified_word_list = list(first_word)
# Swap the letters at the two indices
modified_word_list[first_index], modified_word_list[second_index] = \
modified_word_list[second_index], modified_word_list[first_index]
# Convert the list back to a string
modified_word = "".join(modified_word_list)
# Check if the modified word is equal to the second word
if modified_word == second_word:
return True
# No swap resulted in a matching string
return False
To figure out if two strings can become each other with just one swap, we don't need to try every possible swap. The best method involves checking if simple conditions are met that would allow a swap to work, quickly deciding if a swap is possible, or certainly not.
Here's how the algorithm would work step-by-step:
def buddy_strings(first_string, second_string):
if len(first_string) != len(second_string):
return False
if first_string == second_string:
# Need to check for duplicate chars
seen_characters = set()
for character in first_string:
if character in seen_characters:
return True
seen_characters.add(character)
return False
first_difference_index = -1
second_difference_index = -1
for index in range(len(first_string)):
if first_string[index] != second_string[index]:
if first_difference_index == -1:
first_difference_index = index
elif second_difference_index == -1:
second_difference_index = index
else:
# More than two differences
return False
# Only proceed if there are exactly two indices where characters differ
if first_difference_index != -1 and second_difference_index != -1:
# Ensure swapping chars at difference indices produces the second string
if (first_string[first_difference_index] == second_string[second_difference_index] and\
first_string[second_difference_index] == second_string[first_difference_index]):
return True
return False
Case | How to Handle |
---|---|
s or goal is null | Return false immediately as null strings cannot be compared. |
s or goal is empty string | Return false if one is empty and the other is not, return true only if both are empty. |
s and goal have different lengths | Return false immediately, as strings of different lengths cannot become equal by a single swap. |
s and goal are identical | Check if s contains duplicate characters; if it does, a swap of the same character results in identical string, otherwise return false. |
s and goal differ by more than two characters | Return false as only one swap is allowed. |
Large strings (performance implications) | The algorithm should have O(n) time complexity making it appropriate for larger strings. |
Strings with only 1 character | If the strings are identical and of length 1, and that single char must exist twice, then return true; if they are different, return false. |
String containing Unicode characters | Ensure the character comparison handles unicode correctly, potentially requiring normalization. |