We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Strings
- Making Anagrams
- Discussions
Making Anagrams
Making Anagrams
Sort by
recency
|
772 Discussions
|
Please Login in order to post a comment
public static int makingAnagrams(String s1, String s2) { // Write your code here int freq[] = new int[26]; for(char s : s1.toCharArray()){ freq[s-'a']++; } for(char z : s2.toCharArray()){ freq[z-'a']--; } int count=0; for(int f : freq){ if(f!=0){ count= count+Math.abs(f); } } return count; }
}
//TC: O(N + M), SC: O(1)
public static int makingAnagrams(String s1, String s2) {
This python solution is O(n + m) time and O(k) space.
O(k) because in the worse case all keys will will be unique and it will result in a space of O(n + m)