Check if Strings Can be Made Equal With Operations I
Real-world version: Matching Two Four Letter DialsThe statement here is our own: the same inputs, outputs and technique.
Two dials each show exactly four lowercase letters, given as s1 and s2.
One turn on a dial picks two of its positions that are two apart and swaps the letters in them. So on a dial reading positions 0, 1, 2, 3, a turn swaps positions 0 and 2, or swaps positions 1 and 3. Either dial may be turned, any number of times.
Return true when the two dials can be made to read the same, and false otherwise.
Examples
Example 1
- Input
- s1 = "abcd", s2 = "cdab"
- Output
- true
Turning the first dial at positions 0 and 2 gives "cbad", and turning it again at positions 1 and 3 gives "cdab".
Example 2
- Input
- s1 = "abcd", s2 = "abdc"
- Output
- false
Positions 2 and 3 hold different letters on the two dials, and no turn can move a letter from an even position to an odd one.
Example 3
- Input
- s1 = "abab", s2 = "baba"
- Output
- false
Both pairs hold the same two letters on each dial, so the turns can bring the readings together.
Constraints
- s1.length == 4
- s2.length == 4
- s1 and s2 consist of lowercase English letters only
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def dials_can_match(s1: str, s2: str) -> bool:public boolean dialsCanMatch(String s1, String s2)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's