2840. Check if Strings Can be Made Equal With Operations II LeetCode Solution

In this guide, you will get 2840. Check if Strings Can be Made Equal With Operations II LeetCode Solution with the best time and space complexity. The solution to Check if Strings Can be Made Equal With Operations II 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 II solution in C++
  4. Check if Strings Can be Made Equal With Operations II solution in Java
  5. Check if Strings Can be Made Equal With Operations II solution in Python
  6. Additional Resources
2840. Check if Strings Can be Made Equal With Operations II LeetCode Solution image

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

You are given two strings s1 and s2, both of length n, 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 i < j and the difference j – i is even, 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 = “abcdba”, s2 = “cabdab”
Output: true
Explanation: We can apply the following operations on s1:
– Choose the indices i = 0, j = 2. The resulting string is s1 = “cbadba”.
– Choose the indices i = 2, j = 4. The resulting string is s1 = “cbbdaa”.
– Choose the indices i = 1, j = 5. The resulting string is s1 = “cabdab” = s2.

See also  2182. Construct String With Repeat Limit LeetCode Solution

Example 2:

Input: s1 = “abe”, s2 = “bea”
Output: false
Explanation: It is not possible to make the two strings equal.

Constraints:

n == s1.length == s2.length
1 <= n <= 105
s1 and s2 consist only of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(2 \cdot 26) = O(1)

2840. Check if Strings Can be Made Equal With Operations II LeetCode Solution in C++

class Solution {
 public:
  bool checkStrings(string s1, string s2) {
    vector<vector<int>> count(2, vector<int>(26));

    for (int i = 0; i < s1.length(); ++i) {
      ++count[i % 2][s1[i] - 'a'];
      --count[i % 2][s2[i] - 'a'];
    }

    for (int i = 0; i < 26; ++i)
      if (count[0][i] > 0 || count[1][i] > 0)
        return false;

    return true;
  }
};
/* code provided by PROGIEZ */

2840. Check if Strings Can be Made Equal With Operations II LeetCode Solution in Java

class Solution {
  public boolean checkStrings(String s1, String s2) {
    int[][] count = new int[2][26];

    for (int i = 0; i < s1.length(); ++i) {
      ++count[i % 2][s1.charAt(i) - 'a'];
      --count[i % 2][s2.charAt(i) - 'a'];
    }

    for (int i = 0; i < 26; ++i)
      if (count[0][i] > 0 || count[1][i] > 0)
        return false;

    return true;
  }
}
// code provided by PROGIEZ

2840. Check if Strings Can be Made Equal With Operations II LeetCode Solution in Python

class Solution:
  def checkStrings(self, s1: str, s2: str) -> bool:
    count = [collections.Counter() for _ in range(2)]

    for i, (a, b) in enumerate(zip(s1, s2)):
      count[i % 2][a] += 1
      count[i % 2][b] -= 1

    return (all(freq == 0 for freq in count[0].values()) and
            all(freq == 0 for freq in count[1].values()))
# code by PROGIEZ

Additional Resources

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