You are given two strings s1 and s2, both of length 4, consisting of lowercase English letters.
You can apply the following operation on any of the two strings any number of times:
i and j such that j - i = 2, then swap the two characters at those indices in the string.Return true if you can make the strings s1 and s2 equal, and false otherwise.
Example 1:
Input: s1 = "abcd", s2 = "cdab" Output: true Explanation: We can do the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbad". - Choose the indices i = 1, j = 3. The resulting string is s1 = "cdab" = s2.
Example 2:
Input: s1 = "abcd", s2 = "dacb" Output: false Explanation: It is not possible to make the two strings equal.
Constraints:
s1.length == s2.length == 4s1 and s2 consist only of 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:
This check tries the arrangements reachable by swapping the first character with the third, the second with the fourth, or both. It compares the original text first, then each of those three swapped forms. It returns true on the first match and false when all four forms miss the target.
Here's how the algorithm would work step-by-step:
class Solution:
def canBeEqual(self, first_string: str, second_string: str) -> bool:
# Check if the strings are already identical without any operations.
if first_string == second_string:
return True
first_string_as_list = list(first_string)
# Perform the first possible swap: exchange characters at indices 0 and 2.
first_string_as_list[0], first_string_as_list[2] = first_string_as_list[2], first_string_as_list[0]
if "".join(first_string_as_list) == second_string:
return True
# Undo the swap to revert to the original state for the next check.
first_string_as_list[0], first_string_as_list[2] = first_string_as_list[2], first_string_as_list[0]
# Perform the second possible swap: exchange characters at indices 1 and 3.
first_string_as_list[1], first_string_as_list[3] = first_string_as_list[3], first_string_as_list[1]
if "".join(first_string_as_list) == second_string:
return True
# Now, try both swaps together. The second swap is already applied, so just apply the first.
first_string_as_list[0], first_string_as_list[2] = first_string_as_list[2], first_string_as_list[0]
if "".join(first_string_as_list) == second_string:
return True
return FalseAllowed swaps only rearrange the even positions among themselves and the odd positions among themselves. The strings can be made equal exactly when those two pairs of positions carry the same letters with the same counts. The check therefore compares the even-position multiset and the odd-position multiset.
Here's how the algorithm would work step-by-step:
from collections import Counter
class Solution:
def canBeEqual(self, first_string: str, second_string: str) -> bool:
# Step 1 & 2: Segregate characters from both strings into even and odd indexed groups.
# This separation is crucial because the allowed operations only permit swaps within these groups.
first_string_even_chars = first_string[0::2]
first_string_odd_chars = first_string[1::2]
second_string_even_chars = second_string[0::2]
second_string_odd_chars = second_string[1::2]
# Step 3: Compare the character counts for the even-indexed positions.
# The order doesn't matter, only that the same multiset of characters exists.
if Counter(first_string_even_chars) != Counter(second_string_even_chars):
return False
# Step 4: Similarly, compare the character counts for the odd-indexed positions.
# This ensures the odd positions can also be rearranged to match.
if Counter(first_string_odd_chars) != Counter(second_string_odd_chars):
return False
# Step 5: If both even and odd character sets match, the strings can be made equal.
return True| Case | How to Handle |
|---|---|
| The strings are already equal | Return true immediately, before any swap is attempted. |
| Only the first and third characters need to change places | Swap that pair and return true when the text then matches the target. |
| Only the second and fourth characters need to change places | Undo any earlier even swap, swap the odd pair, and return true on a match. |
| Both pairs must change | Apply both swaps and return true when that arrangement equals the target. |
| The letters on even positions differ as a multiset | Return false, because no allowed swap can move a letter from an even position to an odd one. |
| None of the four reachable arrangements matches | Return false after the original text and the three swapped forms have all failed. |