LeetCode 2839: Check if Strings Can be Made Equal With Operations I Solution
Master LeetCode problem 2839 (Check if Strings Can be Made Equal With Operations I), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2839. Check if Strings Can be Made Equal With Operations I
Problem Explanation
Explanation:
To check if two strings s1 and s2 can be made equal with the given operation, we can use a simple approach.
- We need to check if the characters in
s1ands2are the same, regardless of their order. If the frequency of each character in both strings is the same, then the two strings can be made equal using the given operation. - We can iterate through both strings and count the frequency of each character. If the frequency counts match, we return true; otherwise, we return false.
Time Complexity: O(n) where n is the length of the strings s1 and s2.
Space Complexity: O(1) since we are using a fixed-size array to store the character frequencies.
:
Solution Code
class Solution {
public boolean canBeMadeEqual(String s1, String s2) {
int[] count1 = new int[26];
int[] count2 = new int[26];
for (int i = 0; i < 4; i++) {
count1[s1.charAt(i) - 'a']++;
count2[s2.charAt(i) - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (count1[i] != count2[i]) {
return false;
}
}
return true;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2839 (Check if Strings Can be Made Equal With Operations I)?
This page provides optimized solutions for LeetCode problem 2839 (Check if Strings Can be Made Equal With Operations I) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2839 (Check if Strings Can be Made Equal With Operations I)?
The time complexity for LeetCode 2839 (Check if Strings Can be Made Equal With Operations I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2839 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2839 in Java, C++, or Python.