Skip to main content

2839 - Check if Strings Can be Made Equal With Operations I (Easy)

https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-i/

Problem Statement

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:

  • Choose any two indices i and j such that j - i = 2, then swap the two characters at those indices in the string.

Return trueif you can make the stringss1ands2equal, andfalseotherwise.

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 == 4
  • s1 and s2 consist only of lowercase English letters.

Approach 1: Brute Force

Since we have 4 characters and given we can only swap s1[i] and s1[j] where j - i = 2, we can brute force the solution by checking two cases. If s1[0]s1[0] is not same as s2[0]s2[0], we can only swap s1[0]s1[0] and s1[2]s1[2]. Similarly, we perform the same logic on s1[1]s1[1] and compare s1s1 and s2s2 at the end.

Written by @wingkwong
class Solution {
public:
bool canBeEqual(string s1, string s2) {
if (s1[0] ^ s2[0]) swap(s1[0], s1[2]);
if (s1[1] ^ s2[1]) swap(s1[1], s1[3]);
return s1 == s2;
}
};