In this tutorial, you will learn the writing program in to check string is anagrams or not in java.
Read This : What is Anagram? Write an anagram program in C.
How this Java Program will Behave?
This Program will take two String as an input. And after the comparison it will return output.
If the string is anagram then it will return “Given Strings are anagram” as an output.
And if Strings are not anagram then it will return “Strings are not anagram”.
Anagram Program in Java: Sorting Method
In the below anagram program in Java using the sorting method involves a straightforward approach where two strings are considered anagrams if, when sorted, they are identical. The algorithm begins by checking if the two strings are of the same length; if not, they cannot be anagrams. If they are of equal length, the program converts each string into a character array, sorts these arrays using Java’s built-in sort function, and then converts the arrays back into strings. Finally, it compares the sorted strings: if they are equal, the strings are anagrams; otherwise, they are not. This method is highly effective due to the efficiency of Java’s array sorting, and it simplifies the comparison to a mere string equality check, making it a popular choice for implementing anagram checks in Java applications.
import java.util.*;
public class Main {
public static boolean anagramCheck(String str1, String str2){
boolean status = false;
if (str1.length() != str2.length()) {
status = false;
} else {
char[] arr1 = str1.toLowerCase().toCharArray();
char[] arr2 = str2.toLowerCase().toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
status = Arrays.equals(arr1, arr2);
}
return status;
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Please give First String : ");
String str1= sc.nextLine();
Scanner sc1= new Scanner(System.in);
System.out.print("Please give Second String : ");
String str2= sc.nextLine();
if(anagramCheck(str1,str2)){
System.out.print("String are anagram");
}else{
System.out.print("String are not anagram");
}
}
}
Output 1:
Please give First String : quescol
Please give Second String : colques
String are anagram
Output 2:
Please give First String : quescol
Please give Second String : hello
String are not anagram
Anagram Program in Java: Counting Character
In Java, the counting character method for detecting anagrams involves tallying the frequency of each character in two strings and then comparing these frequencies. This method is effective because anagrams contain the same characters in the same quantities. The algorithm creates an array of integers to count occurrences of each character for both strings, typically assuming the character set is ASCII and thus using an array of size 256 (one for each ASCII character). Each character in the first string increments its corresponding index in the array, while each character in the second string decrements the same index. If all array values are zero at the end of this process, the strings are anagrams; otherwise, they are not. This approach is highly efficient, especially for strings composed of limited character sets, as it avoids the overhead of sorting and directly compares character counts.
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Please give First String : ");
String str1= sc.nextLine();
Scanner sc1= new Scanner(System.in);
System.out.print("Please give Second String : ");
String str2= sc.nextLine();
final int CHAR_COUNT = 256;
boolean areAnagrams = true;
if (str1.length() != str2.length()) {
areAnagrams = false;
} else {
int[] count = new int[CHAR_COUNT];
for (int i = 0; i < str1.length(); i++) {
count[str1.charAt(i)]++;
count[str2.charAt(i)]--;
}
for (int i = 0; i < CHAR_COUNT; i++) {
if (count[i] != 0) {
areAnagrams = false;
break;
}
}
}
if (areAnagrams) {
System.out.println("String are anagram");
} else {
System.out.println("String are not anagram");
}
}
}
Output 1:
Please give First String : quescol
Please give Second String : colques
String are anagram
Output 2:
Please give First String : quescol
Please give Second String : hello
String are not anagram
Anagram Program in Java: Using Hashing
In Java, using hashing to determine if two strings are anagrams involves leveraging a hash map to count the frequency of each character in both strings. The program starts by creating a HashMap where each key represents a character, and the associated value is the count of that character in the string. For the first string, the algorithm increments the count of each character in the map. For the second string, it decrements the count for each character. After processing both strings, the program checks the HashMap; if all counts are zero (i.e., for each character added from the first string, there was a corresponding subtraction from the second), the strings are anagrams. This method effectively manages character counts even with diverse character sets and handles varying string lengths efficiently, making it robust for anagram detection in applications that require handling complex and large datasets.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Please give First String : ");
String str1= sc.nextLine();
Scanner sc1= new Scanner(System.in);
System.out.print("Please give Second String : ");
String str2= sc.nextLine();
boolean areAnagrams = true;
if (str1.length() != str2.length()) {
areAnagrams = false;
} else {
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < str1.length(); i++) {
map.put(str1.charAt(i), map.getOrDefault(str1.charAt(i), 0) + 1);
map.put(str2.charAt(i), map.getOrDefault(str2.charAt(i), 0) - 1);
}
for (int count : map.values()) {
if (count != 0) {
areAnagrams = false;
break;
}
}
}
if (areAnagrams) {
System.out.println("Strings are anagram.");
} else {
System.out.println("Strings are not anagram.");
}
}
}
Output 1:
Please give First String : quescol
Please give Second String : colques
String are anagram
Output 2:
Please give First String : quescol
Please give Second String : hello
String are not anagram