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

In this guide, you will get 2839. Check if Strings Can be Made Equal With Operations I LeetCode Solution with the best time and space complexity. The solution to Check if Strings Can be Made Equal With Operations I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if Strings Can be Made Equal With Operations I solution in C++
  4. Check if Strings Can be Made Equal With Operations I solution in Java
  5. Check if Strings Can be Made Equal With Operations I solution in Python
  6. Additional Resources
2839. Check if Strings Can be Made Equal With Operations I LeetCode Solution image

Problem Statement of Check if Strings Can be Made Equal With Operations I

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 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.

See also  3446. Sort Matrix by Diagonals LeetCode Solution

Constraints:

s1.length == s2.length == 4
s1 and s2 consist only of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2839. Check if Strings Can be Made Equal With Operations I LeetCode Solution in C++

class Solution {
 public:
  bool canBeEqual(string s1, string s2) {
    for (const string& a : swappedStrings(s1))
      for (const string& b : swappedStrings(s2))
        if (a == b)
          return true;
    return false;
  }

 private:
  vector<string> swappedStrings(const string& s) {
    vector<char> chars(s.begin(), s.end());
    return {s, string({chars[2], chars[1], chars[0], chars[3]}),
            string({chars[0], chars[3], chars[2], chars[1]}),
            string({chars[2], chars[3], chars[0], chars[1]})};
  }
};
/* code provided by PROGIEZ */

2839. Check if Strings Can be Made Equal With Operations I LeetCode Solution in Java

class Solution {
  public boolean canBeEqual(String s1, String s2) {
    for (final String a : swappedStrings(s1))
      for (final String b : swappedStrings(s2))
        if (a.equals(b))
          return true;
    return false;
  }

  private List<String> swappedStrings(final String s) {
    List<String> res = new ArrayList<>();
    char[] chars = s.toCharArray();
    res.add(s);
    res.add(new String(new char[] {chars[2], chars[1], chars[0], chars[3]}));
    res.add(new String(new char[] {chars[0], chars[3], chars[2], chars[1]}));
    res.add(new String(new char[] {chars[2], chars[3], chars[0], chars[1]}));
    return res;
  }
}
// code provided by PROGIEZ

2839. Check if Strings Can be Made Equal With Operations I LeetCode Solution in Python

class Solution:
  def canBeEqual(self, s1: str, s2: str) -> bool:
    def swappedStrings(s: str) -> list[str]:
      chars = list(s)
      return [chars,
              ''.join([chars[2], chars[1], chars[0], chars[3]]),
              ''.join([chars[0], chars[3], chars[2], chars[1]]),
              ''.join([chars[2], chars[3], chars[0], chars[1]])]

    return any(a == b
               for a in swappedStrings(s1)
               for b in swappedStrings(s2))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.