Show problems as
Classic problem #2839EasyString

Check if Strings Can be Made Equal With Operations I

Real-world version: Matching Two Four Letter Dials
Tracked in this browser only
Read Check if Strings Can be Made Equal With Operations I on LeetCode(opens in a new tab)

The 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.

Python
def dials_can_match(s1: str, s2: str) -> bool:
Java
public boolean dialsCanMatch(String s1, String s2)