Check if Strings Can be Made Equal With Operations II - Problem

You are given two strings s1 and s2 of equal length n, both consisting of lowercase English letters.

Your mission is to determine if you can transform either string into the other using a special swapping operation. You can perform this operation any number of times on either string:

  • Operation: Choose any two indices i and j where i < j and the difference j - i is even, then swap the characters at positions i and j.

The key insight is that this constraint creates two separate "groups" within each string:

  • Even positions: 0, 2, 4, 6, ... (can only swap with other even positions)
  • Odd positions: 1, 3, 5, 7, ... (can only swap with other odd positions)

Return true if the strings can be made equal, false otherwise.

Input & Output

example_1.py โ€” Basic Case
$ Input: s1 = "abcdxyz", s2 = "cbadfgh"
โ€บ Output: false
๐Ÿ’ก Note: Even positions: s1 has [a,c,x,z], s2 has [c,a,f,h]. The characters f,h,x,z don't match between the two groups, so no amount of swapping can make them equal.
example_2.py โ€” Successful Match
$ Input: s1 = "abcd", s2 = "cdab"
โ€บ Output: true
๐Ÿ’ก Note: Even positions: s1 has [a,c], s2 has [c,a]. Odd positions: s1 has [b,d], s2 has [d,b]. Both groups have matching character frequencies, so we can swap within each group to make strings equal.
example_3.py โ€” Edge Case
$ Input: s1 = "ab", s2 = "ba"
โ€บ Output: false
๐Ÿ’ก Note: Even positions: s1 has [a] at position 0, s2 has [b] at position 0. Odd positions: s1 has [b] at position 1, s2 has [a] at position 1. Since characters at even and odd positions can't be swapped with each other, these strings cannot be made equal.

Constraints

  • 1 โ‰ค n โ‰ค 105 where n is the length of both strings
  • Both strings consist of lowercase English letters only
  • s1.length == s2.length (equal length guaranteed)

Visualization

Tap to expand
The Even-Odd Position ConstraintString: "a b c d e f"Positions: 0 1 2 3 4 5a0b1c2d3e4f5Even Position Group (Blue Line)Positions: 0, 2, 4, ...Characters can swap: a โ†” c โ†” eaceOdd Position Group (Red Line)Positions: 1, 3, 5, ...Characters can swap: b โ†” d โ†” fbdf๐Ÿšซ No Cross-Group Swapping AllowedEven positions cannot swap with odd positionsThis constraint creates two independent character rearrangement groups
Understanding the Visualization
1
Identify the Constraint
Positions 0,2,4,... form one group, positions 1,3,5,... form another
2
Apply the Insight
Characters can only swap within their position parity group
3
Count Frequencies
For each group, count character frequencies in both strings
4
Compare Groups
Strings can be equal if and only if both groups have matching frequency distributions
Key Takeaway
๐ŸŽฏ Key Insight: The even-odd position constraint creates two independent groups. Strings can be made equal if and only if both groups have identical character frequency distributions!
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
42.0K Views
Medium-High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen