An anagram is a type of word, the result of rearra…
Unknown - Jul 1, 2014
An anagram is a type of word, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.
For example: orchestra can be rearranged into carthorse or cat can be rearranged into act.
We can find out the anagram strings using below algorithm:
public static boolean isAnagram(String str1, String str2) {
if (str1 == null || str2 == null) {
return false;
} else if (str1.length() != str2.length()) {
return false;
}
Map map = new HashMap();
for (int i = 0; i < str1.length(); i++) {
char characters = str1.charAt(i);
int charInStr1 = map.containsKey(characters) ? map.get(characters) : 0;
map.put(characters, ++charInStr1);
char charFromStr2 = str2.charAt(i);
int charsInRight = map.containsKey(charFromStr2) ? map.get(charFromStr2) : 0;
map.put(charFromStr2, –charsInRight);
}
for (int occurrences : map.values()) {
if (occurrences != 0) {
return false;
}
}
return true;
}
I found below useful links for more information
Write program to find if Strings are anagram