Given an input of two strings, determine if they are anagrams of each other.
A first approach to this involves sorting the two strings by their characters. Then, a simple equality check can be done to see if the sorted strings match.
Approach 2: Use char[256]
Read full article from How to Check if Two Strings Are Anagrams
A first approach to this involves sorting the two strings by their characters. Then, a simple equality check can be done to see if the sorted strings match.
Approach 2: Use char[256]
public boolean areAnagrams(String first, String second) { if(first.length() != second.length()) return false; int alphabet1[] = new int[26]; int alphabet2[] = new int[26]; for(int x = 0; x < first.length(); x++) { if(Character.isLetter(first.charAt(x)) && Character.isLetter(second.charAt(x))) { alphabet1[first.charAt(x) - 'a']++; alphabet2[second.charAt(x) - 'a']++; } else return false; } for(int x = 0; x < 26; x++) { if(alphabet1[x] != alphabet2[x]) return false; } return true; }From Coding Interviews: Questions, Analysis & Solutions
Read full article from How to Check if Two Strings Are Anagrams