A vintage combination lock has two identical dials, s1 and s2, each displaying a row of exactly 4 lowercase-letter symbols in fixed peg slots numbered 0 to 3. On either dial, you may repeatedly swap the symbols sitting in two peg slots that are exactly two positions apart — that is, slots i and i+2 — performing this move on either dial, any number of times, in any order. Determine whether the two dials can be turned into displaying the exact same 4-symbol combination.
Two lines.
s1 of length 4.s2 of length 4.Both strings consist only of lowercase English letters ('a'-'z').
Print true if the dials can be made to show the same combination, or false otherwise.
s1.length == s2.length == 4s1 and s2 consist only of lowercase English letters.Example 1
Input
abcd cbad
Expected
true
Explanation
Swapping slots 0 and 2 of s1="abcd" turns it into "cbad", which already equals s2, so the dials can be made identical — output true.
Example 2
Input
abcd abdc
Expected
false
Explanation
s1's symbols at slots {0,2} are {a,c} while s2's symbols at slots {0,2} are {a,d}. Since slot-0/2 swaps only rearrange characters within that same pair, s1's pair {a,c} can never become {a,d}, so no sequence of swaps can equalize the dials — output false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →