· 1 min read
Blind 75 - Valid Anagrams
check if the length is the same.<br> find the difference in the count of letters in both numbers.<br> since there are only 26 letters, you can count in a single int array of 26. <br>index = char - 'a'
Video
https://youtu.be/7KHKFqLPMbsProblem and Constraints
Check if two numbers are anagrams or not.
Anagram means one word can be made by re-arranging another word.
All Approaches and Explanations in English
O(n) time complexity. O(1) space complexity
check if the length is the same. find the difference in the count of letters in both numbers. since there are only 26 letters, you can count in a single int array of 26. index = char - ‘a’
Code, if any
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()){
return false;
}
int[] counts = new int[26];
for(int i = 0; i<s.length(); i++){
counts[s.charAt(i) - 'a']++;
counts[t.charAt(i) - 'a']--;
}
for(int i = 0; i<counts.length; i++){
if(counts[i]!=0){
return false;
};
}
return true;
}
}Related posts
- Blind 75 - Reverse LinkedList Nov 16, 2022
- Blind 75 - Same Tree Nov 16, 2022
- Blind 75 - Product of Array Except Self Nov 4, 2022
Get new posts by email
No spam. Unsubscribe anytime. Or grab the RSS feed.
Mailto-based for now. ButtonDown / ConvertKit can replace this form later.