Check if Strings Can be Made Equal With Operations I - Problem
You are given two strings s1 and s2, both of length 4, consisting of lowercase English letters.
Your task is to determine if you can make these strings identical using a special swap operation. You can perform the following operation any number of times on either string:
- Choose any two indices
iandjsuch thatj - i = 2(exactly 2 positions apart) - Swap the characters at positions
iandj
Example: In string "abcd", you can swap positions 0 and 2 (characters 'a' and 'c') or positions 1 and 3 (characters 'b' and 'd').
Return true if you can make the strings equal, false otherwise.
Input & Output
example_1.py — Basic swap case
$
Input:
s1 = "abcd", s2 = "cdab"
›
Output:
true
💡 Note:
We can swap positions 0↔2 in s1 to get "cbad", then swap positions 1↔3 to get "cdab" which matches s2. Alternatively, both strings have the same characters in even positions [a,c] and odd positions [b,d] when sorted.
example_2.py — No solution case
$
Input:
s1 = "abcd", s2 = "dacb"
›
Output:
false
💡 Note:
Even positions: s1 has [a,c], s2 has [d,c]. Odd positions: s1 has [b,d], s2 has [a,b]. Since the character sets don't match for both groups, no amount of swapping can make them equal.
example_3.py — Already equal case
$
Input:
s1 = "abcd", s2 = "abcd"
›
Output:
true
💡 Note:
The strings are already identical, so no swaps are needed. Both have matching even [a,c] and odd [b,d] position characters.
Constraints
- s1.length == s2.length == 4
- s1 and s2 consist of lowercase English letters only
- Only swaps between positions with difference of exactly 2 are allowed
Visualization
Tap to expand
Understanding the Visualization
1
Initial Setup
You have two hands of 4 cards each, positions 0,1,2,3
2
Swap Rules
You can only swap cards at positions 0↔2 and 1↔3
3
Group Recognition
This creates two independent groups: {0,2} and {1,3}
4
Solution
Check if both hands have the same cards in each group
Key Takeaway
🎯 Key Insight: Swapping positions 2 apart creates two independent groups. Just compare the sorted cards in each group!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code