In this post, we will write java programs to check whether two strings are anagram or not. We will discuss 5 different methods to check for anagram strings. Before moving ahead with Anagram program in java , first we need to know what does anagram mean?
Read Also : 5 ways of String Concatenation in Java with Example
Now we know what does anagram mean. Let understand the question by writing example.
Input : "now","own"
Output : true
Input : "ton" , "not"
Output : true
Input : "hello" , "he"
Output : false
1. Convert the two strings into uppercase and remove all white spaces.
2. Convert the two strings into char arrays using toCharArray().
3. Sort the two character arrays using sort() method of java.util.Arrays class.
4. After sorting, we compare both the arrays using equals() method.
Pseudo Code for Anagram Program in java using Iterative method:
1. In this method we go on checking whether each character of first string is present in second string.
2. If the character is present in second string, we remove the character from the second string, and proceed to the next character of first string.
3. If any character of first string is not present in second string, we break the loop and result would be first string is not anagram of second string.
Pseudo Code for Anagram Program in java using HashMap method:
1. Create one HashMap object with character as key and character occurrences as value.
2. If the character is present in first string , we increment character count by 1.
3. If the character is present in second string , we decrement character count by 1.
4. Iterate through HashMap , check the count of each character in the map , if count is not equal to 0 then return false.
please mention in the comments, if you have any other way to code anagram program in java.
What is Anagram ?
If two strings contain same set of characters but in different order then the two strings are called anagrams.
for example :
1. "now" and "own"
2. "ton" and "not"
3. "fiber" and "brief"
Read Also : 5 ways of String Concatenation in Java with Example
Now we know what does anagram mean. Let understand the question by writing example.
Input : "now","own"
Output : true
Input : "ton" , "not"
Output : true
Input : "hello" , "he"
Output : false
Pseudo Code for Anagram Program in java using sort() and equals() method:
1. Convert the two strings into uppercase and remove all white spaces.
2. Convert the two strings into char arrays using toCharArray().
3. Sort the two character arrays using sort() method of java.util.Arrays class.
4. After sorting, we compare both the arrays using equals() method.
import java.util.*; import java.io.*; public class Anagram { public static void main (String[] args) throws java.lang.Exception { boolean result = isAnagram("now","own"); System.out.println(result); } public static boolean isAnagram(String first, String second) { // remove all whitespaces and convert strings to lowercase first = first.replaceAll("\\s", "").toLowerCase(); second = second.replaceAll("\\s", "").toLowerCase(); /* check whether string lengths are equal or not, if unequal then not anagram */ if (first.length() != second.length()) return false; // convert string to char array char[] firstArray = first.toCharArray(); char[] secondArray = second.toCharArray(); // sort both the arrays Arrays.sort(firstArray); Arrays.sort(secondArray); // checking whether both strings are equal or not return Arrays.equals(firstArray,secondArray); } }
Pseudo Code for Anagram Program in java using Iterative method:
1. In this method we go on checking whether each character of first string is present in second string.
2. If the character is present in second string, we remove the character from the second string, and proceed to the next character of first string.
3. If any character of first string is not present in second string, we break the loop and result would be first string is not anagram of second string.
import java.util.*; import java.lang.*; import java.io.*; public class Anagram { public static void main (String[] args) throws java.lang.Exception { boolean result = isAnagram("now","own"); System.out.println(result); } public static boolean isAnagram(String first, String second) { // remove all whitespaces and convert strings to lowercase first = first.replaceAll("\\s", "").toLowerCase(); second = second.replaceAll("\\s", "").toLowerCase(); /* check whether string lengths are equal or not, if unequal then not anagram */ if (first.length() != second.length()) return false; // convert first string to char array char[] firstArray = first.toCharArray(); // check whether each character of firstArray is present in second string for (char c : firstArray) { int index = second.indexOf(c); // indexOf function returns -1 if the character is not found if (index == -1) return false; // if character is present in second string, remove that character from second string second = second.substring(0,index) + second.substring(index+1, second.length()); } return true; } }
Pseudo Code for Anagram Program in java using HashMap method:
1. Create one HashMap object with character as key and character occurrences as value.
2. If the character is present in first string , we increment character count by 1.
3. If the character is present in second string , we decrement character count by 1.
4. Iterate through HashMap , check the count of each character in the map , if count is not equal to 0 then return false.
import java.util.*; import java.io.*; public class Anagram { public static void main (String[] args) throws java.lang.Exception { isAnagram("now","own"); } public static void isAnagram(String stringOne, String stringTwo) { //Remove all white space & case differences String strCopyOne = stringOne.replaceAll("\\s", "").toUpperCase(); String strCopyTwo = stringTwo.replaceAll("\\s", "").toUpperCase(); //If the strings aren’t the same length, not anagrams if (strCopyOne.length() != strCopyTwo.length()) { System.out.println(stringOne + " and " + stringTwo + " are not anagrams."); return; } //HashMap to store the number of characters HashMap mapOne = createMapKeys(strCopyOne); HashMap mapTwo = createMapKeys(strCopyTwo); if (mapOne.equals(mapTwo)) { System.out.println(stringOne + " and " + stringTwo + " are anagrams"); } else { System.out.println(stringOne + " and " + stringTwo + " are not anagrams."); } } public static HashMap createMapKeys(String str) { HashMap map = new HashMap(); for (int i = 0; i < str.length(); i++) { if (map.containsKey(str.charAt(i))) { int count = (int) map.get(str.charAt(i)); map.put(str.charAt(i), count + 1); } else { map.put(str.charAt(i), 0); } } return map; } }
please mention in the comments, if you have any other way to code anagram program in java.