Magicsheet logo

Check if Strings Can be Made Equal With Operations I

Easy
100%
Updated 6/1/2025

Asked by 1 Company

Topics

Check if Strings Can be Made Equal With Operations I

What is this problem about?

This is an "Easy" string problem where you are given two strings of length 4. You can swap characters at indices ii and jj if ji=2j - i = 2. This means you can swap index 0 with index 2, and index 1 with index 3. You need to determine if you can make the two strings equal using these specific swaps.

Why is this asked in interviews?

Citrix and other companies use this to test basic logical reasoning. Because the string length is fixed at 4 and the operations are very limited, it's a test of whether you can identify all possible states or simply check the two possible swaps. It evaluates your ability to handle small, constrained problem sets without over-engineering.

Algorithmic pattern used

The pattern here is simple Parity-based Matching or Simulation. Because you can only swap indices with a distance of 2, index 0 can only ever be at position 0 or 2, and index 1 can only ever be at position 1 or 3. The problem reduces to checking if the set of characters at even indices matches, and the set of characters at odd indices matches.

Example explanation

s1="abcd",s2="cbad"s1 = "abcd", s2 = "cbad"

  1. Even indices in s1s1: {a, c}. Even indices in s2s2: {c, a}. (Match!)
  2. Odd indices in s1s1: {b, d}. Odd indices in s2s2: {b, d}. (Match!) Result: True. s1="abcd",s2="abcd"s1 = "abcd", s2 = "abcd"
  3. No swaps needed. (Match!) Result: True.

Common mistakes candidates make

The most common mistake is overthinking and trying to use a general-purpose sorting or backtracking algorithm. Another is forgetting that since the length is only 4, you can just manually check the four possible combinations of swaps (swap 0-2, swap 1-3, swap both, swap none).

Interview preparation tip

For problems with very small fixed constraints (like length 4), don't be afraid to use a direct, exhaustive check. It's often faster to write and less prone to bugs than a generalized solution.

Similar Questions