Wonders can be done with Strings, especially in Java. In this article, we will see how to implement the Anagram program in Java in the following order:
- What is an Anagram?
- Anagram Program In Java Using sort() and equals() Methods
- Check if Two Strings Are Anagram Using Array
What is an Anagram?
An Anagram is a word which is formed by rearranging or shuffling of letters in another word, the most important property in Anagram is that all the letters have to be used only once. For example, let’s take the popular anagram, LISTEN is an anagram of SILENT.
In this Anagram Program in Java, we will look into some of the possible ways to check if two Strings are Anagram or Not.
Anagram Program In Java Using sort() and equals() Methods
First, we clean the input by removing all white spaces from the given two strings and change the case of all characters of both the strings to lower case so that the case of both input strings will be ignored. After cleaning the input strings, we convert them to character array and sort them using sort() method of java.util.Arrays class.
After sorting, we compare both the arrays using equals() method of the same Array class. This method will return true if both arrays have the same set of characters. Below is the complete anagram program using sort() and equals() methods.
public class AnagramProgram { static void isAnagram(String s1, String s2) { //Removing all white spaces from s1 and s2 String copyOfs1 = s1.replaceAll("s", ""); String copyOfs2 = s2.replaceAll("s", ""); //Initially setting status as true boolean status = true; if(copyOfs1.length() != copyOfs2.length()) { //Setting status as false if copyOfs1 and copyOfs2 doesn't have same length status = false; } else { //Changing the case of characters of both copyOfs1 and copyOfs2 and converting them to char array char[] s1Array = copyOfs1.toLowerCase().toCharArray(); char[] s2Array = copyOfs2.toLowerCase().toCharArray(); //Sorting both s1Array and s2Array Arrays.sort(s1Array); Arrays.sort(s2Array); //Checking whether s1Array and s2Array are equal status = Arrays.equals(s1Array, s2Array); } //Output if(status) { System.out.println(s1+" and "+s2+" are anagrams"); } else { System.out.println(s1+" and "+s2+" are not anagrams"); } } public static void main(String[] args) { isAnagram("Mother In Law", "Hitler Woman"); isAnagram("keEp", "peeK"); isAnagram("SiLeNt CAT", "LisTen AcT"); isAnagram("Debit Card", "Bad Credit"); isAnagram("School MASTER", "The ClassROOM"); isAnagram("DORMITORY", "Dirty Room"); isAnagram("ASTRONOMERS", "NO MORE STARS"); isAnagram("Toss", "Shot"); isAnagram("joy", "enjoy"); } }
Check if Two Strings Are Anagram Using Array
This is the simplest of all methods. After getting the strings from the user and we need to first remove all the white space and convert them into the lower case for a non-case sensitive comparison. Now convert them into a character array and sort them alphabetically. Just compare both arrays has the same elements.
package com.javainterviewpoint; import java.util.Arrays; import java.util.Scanner; public class AnagramChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Getting the input string from the user System.out.print("Enter the First String : "); String s1 = scanner.nextLine(); System.out.print("Enter the second String : "); String s2 = scanner.nextLine(); if(checkAnagram(s1, s2)) System.out.println(s1+" and "+s2+" are Anagrams"); else System.out.println(s1+" and "+s2+" are NOT Anagrams"); scanner.close(); } public static boolean checkAnagram(String s1, String s2) { // Remove all the white space s1 = s1.replaceAll("s", ""); s2 = s2.replaceAll("s", ""); // Check if both length matches if(s1.length() != s2.length()) return false; else { // Convert both Strings into lower case and into Character Array char[] arr1 = s1.toLowerCase().toCharArray(); char[] arr2 = s2.toLowerCase().toCharArray(); // Sort both Character Array Arrays.sort(arr1); Arrays.sort(arr2); // Check if both arrays are equal return (Arrays.equals(arr1, arr2)); } } }
With this, we come to an end of this Anagram Program in Java article. I hope you got an understanding of what exactly is an anagram and how to write an anagram program in Java.
Check out the Java Course training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Anagram Program in Java” blog and we will get back to you as soon as possible.