All problems
0858EasyString

Matching Two Four Letter Dials

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2839Check if Strings Can be Made Equal With Operations I

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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