You are given two strings s1 and s2, both of length 4, consisting of lowercase English letters.
You can apply the following operation on any of the two strings any number of times:
i and j such that j - i = 2, then swap the two characters at those indices in the string.Return true if you can make the strings s1 and s2 equal, and false otherwise.
Example 1:
Input: s1 = "abcd", s2 = "cdab" Output: true Explanation: We can do the following operations on s1: - Choose the indices i = 0, j = 2. The resulting string is s1 = "cbad". - Choose the indices i = 1, j = 3. The resulting string is s1 = "cdab" = s2.
Example 2:
Input: s1 = "abcd", s2 = "dacb" Output: false Explanation: It is not possible to make the two strings equal.
Constraints:
s1.length == s2.length == 4s1 and s2 consist only of lowercase English letters.In #2839 Check if Strings Can be Made Equal With Operations I, the allowed operations let you swap characters at fixed index pairs. Observing these swaps reveals that characters at even indices can only move among even positions, and characters at odd indices can only move among odd positions. This means the string effectively splits into two independent groups.
To determine if s1 can become s2, compare the characters in the even positions of both strings and the characters in the odd positions of both strings. If the characters within each group match (order may vary but frequency must be the same), the transformation is possible.
This approach avoids simulating swaps and instead relies on grouping and comparison of characters. Since the string length is fixed and small, the solution runs in O(n) time with O(1) extra space.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Group Even and Odd Indices Comparison | O(n) | O(1) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
<div class="_1l1MA">Since the strings are very small you can try a brute-force approach.</div>
<div class="_1l1MA">There are only <code>2</code> different swaps that are possible in a string.</div>
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
The allowed operations restrict swaps to fixed index pairs. This creates two independent groups: indices 0 and 2, and indices 1 and 3. Characters cannot move between these groups, so each group must independently match the target string.
Yes, similar string manipulation and constraint-based swapping problems appear in coding interviews. They test your ability to identify patterns, invariants, and simplify the problem without brute-force simulation.
A simple comparison using arrays, sorting, or frequency counting works well. Because the groups are small, you can directly compare the characters from the even and odd positions without needing complex data structures.
The optimal approach groups characters by index parity. Since swaps only occur between positions (0,2) and (1,3), even indices can only swap with even ones and odd with odd ones. If the characters in these groups match between the two strings, the transformation is possible.