2840. Check if Strings Can be Made Equal With Operations II
</> Solution
/**
* Problem: LeetCode 2840 - Check if Strings Can be Made Equal With Operations II
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public boolean checkStrings(String s1, String s2) {
int n = s1.length();
int[] countEven = new int[26];
int[] countOdd = new int[26];
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
countEven[s1.charAt(i) - 'a']++;
countEven[s2.charAt(i) - 'a']--;
} else {
countOdd[s1.charAt(i) - 'a']++;
countOdd[s2.charAt(i) - 'a']--;
}
}
for (int i = 0; i < 26; i++) {
if (countEven[i] != 0 || countOdd[i] != 0) {
return false;
}
}
return true;
}
}
"""
Problem: LeetCode 2840 - Check if Strings Can be Made Equal With Operations II
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from collections import Counter
class Solution:
def checkStrings(self, s1: str, s2: str) -> bool:
return (
Counter(s1[0::2]) == Counter(s2[0::2]) and
Counter(s1[1::2]) == Counter(s2[1::2])
)
/**
* Problem: LeetCode 2840 - Check if Strings Can be Made Equal With Operations II
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
bool checkStrings(string s1, string s2) {
int n = s1.length();
vector<int> countEven(26, 0);
vector<int> countOdd(26, 0);
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
countEven[s1[i] - 'a']++;
countEven[s2[i] - 'a']--;
} else {
countOdd[s1[i] - 'a']++;
countOdd[s2[i] - 'a']--;
}
}
for (int i = 0; i < 26; ++i) {
if (countEven[i] != 0 || countOdd[i] != 0) {
return false;
}
}
return true;
}
};
/**
* Problem: LeetCode 2840 - Check if Strings Can be Made Equal With Operations II
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var checkStrings = function(s1, s2) {
const n = s1.length;
const countEven = new Int32Array(26);
const countOdd = new Int32Array(26);
for (let i = 0; i < n; i++) {
const code1 = s1.charCodeAt(i) - 97;
const code2 = s2.charCodeAt(i) - 97;
if (i % 2 === 0) {
countEven[code1]++;
countEven[code2]--;
} else {
countOdd[code1]++;
countOdd[code2]--;
}
}
for (let i = 0; i < 26; i++) {
if (countEven[i] !== 0 || countOdd[i] !== 0) {
return false;
}
}
return true;
};