4 Ways to Check String is Anagram in Java

In this tutorial I will tell you the four different ways to check string is anagram in Java or not.

Two strings are anagram if they contains same characters in different order. For example word and odwr are anagrams.

 

Ways to Check String is Anagram in Java

 

Ways to Check String is Anagram in Java

Method 1

In this method we sort the strings using Arrays.sort() method and then compare them using Arrays.equals() method. If strings are equal then they are anagram.

package com;

import java.util.Arrays;
import java.util.Scanner;

public class AnagramInJava {
	public static void main(String args[]){
		String s1, s2;
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter first string:");
		s1 = sc.nextLine();
		
		System.out.println("\nEnter second string:");
		s2 = sc.nextLine();

		if(s1.length() == s2.length()){
			checkAnagram(s1, s2);
		}
		else{
			System.out.println("\nStrings are not Anagram");	
		}
		
		sc.close();
	}
	
	static void checkAnagram(String s1, String s2){
		char c1[] = s1.toCharArray();
		char c2[] = s2.toCharArray();
		
		Arrays.sort(c1);
		Arrays.sort(c2);
		
		if(Arrays.equals(c1, c2)){
			System.out.println("\nStrings are Anagram");
		}
		else{
			System.out.println("\nStrings are not Anagram");
		}		
	}
}

 

Output

Enter first string:
adsa

Enter second string:
asad

Strings are Anagram

 

Method 2

In this method we will count the occurrence of each character in first and second string. If the occurrence of characters are equal then strings are anagram.

package com;

import java.util.Scanner;

public class AnagramInJava {
	public static void main(String args[]){
		String s1, s2;
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter first string:");
		s1 = sc.nextLine();
		
		System.out.println("\nEnter second string:");
		s2 = sc.nextLine();

		if(s1.length() == s2.length()){
			checkAnagram(s1, s2);
		}
		else{
			System.out.println("\nStrings are not Anagram");	
		}
		
		sc.close();
	}
	
	static void checkAnagram(String s1, String s2){
		char c1[] = s1.toCharArray();
		char c2[] = s2.toCharArray();
		
		int count1[] = new int[256];
		int count2[] = new int[256];
		
		boolean flag = true;
		
		for(int i = 0; i<c1.length; ++i){
			count1[c1[i]]++;
			count2[c2[i]]++;
		}
		
		for(int i = 0; i<256; ++i){
			if(count1[i] != count2[i]){
				flag = false;
				break;
			}
		}
		
		if(flag){
			System.out.println("\nStrings are Anagram");
		}
		else{
			System.out.println("\nStrings are not Anagram");
		}		
	}
}

 

Method 3

In this method we will pick one character form first string and remove it from second string. Repeat the process for all characters. If length of second string is zero that means both the strings are anagram.

I used substring() method of String class to remove the character. We can also use deleteCharAt() method of StringBuilder class.

package com;

import java.util.Arrays;
import java.util.Scanner;

public class AnagramInJava {
	public static void main(String args[]){
		String s1, s2;
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter first string:");
		s1 = sc.nextLine();
		
		System.out.println("\nEnter second string:");
		s2 = sc.nextLine();

		if(s1.length() == s2.length()){
			checkAnagram(s1, s2);
		}
		else{
			System.out.println("\nStrings are not Anagram");	
		}
		
		sc.close();
	}
	
	static void checkAnagram(String s1, String s2){
		char c1[] = s1.toCharArray();
		int index;
		boolean flag = true;
		
		for(char c : c1){
			index = s2.indexOf(c);
			
			if(index != -1){
				s2 = s2.substring(0, index) + s2.substring(index+1, s2.length());
			}
			else{
				flag = false;
				break;
			}
		}
		
		if(flag){
			System.out.println("\nStrings are Anagram");
		}
		else{
			System.out.println("\nStrings are not Anagram");
		}		
	}
}

 

Method 4

In this method I have used HashMap with character as key and integer as value. First pick a character from first string and using this character as key increase the value in hashmap by 1. For second string we will decrease the value in hashmap by 1 for a character as key.

For example character a is encountered in first string so value in hashmap will become 1 for key a. The initial value was 0. Now if same character a is encountered in second string then value will become 0. We will repeat the same process for all the characters in first and second string.

Finally we will check if all the values in hashmap are 0 then the strings are anagram.

package com;

import java.util.HashMap;
import java.util.Scanner;

public class AnagramInJava {
	public static void main(String args[]){
		String s1, s2;
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter first string:");
		s1 = sc.nextLine();
		
		System.out.println("\nEnter second string:");
		s2 = sc.nextLine();

		if(s1.length() == s2.length()){
			checkAnagram(s1, s2);
		}
		else{
			System.out.println("\nStrings are not Anagram");	
		}
		
		sc.close();
	}
	
	static void checkAnagram(String s1, String s2){
		char c1[] = s1.toCharArray();
		char c2[] = s2.toCharArray();
		
		boolean flag = true;
		int count;
		HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
		
		for(int i = 0; i<c1.length; ++i){
			count = 0;
			
			if(hashMap.containsKey(c1[i])){
				count = hashMap.get(c1[i]);
			}
			
			hashMap.put(c1[i], ++count);
			
			count = 0;
			
			if(hashMap.containsKey(c2[i])){
				count = hashMap.get(c2[i]);
			}
			
			hashMap.put(c2[i], --count);
		}
		
		for(int i : hashMap.values()){
			if(i != 0){
				flag = false;
				break;
			}
		}
		
		if(flag){
			System.out.println("\nStrings are Anagram");
		}
		else{
			System.out.println("\nStrings are not Anagram");
		}
	}
}

 

Please mention in the comment section if you know any other good way to check string is anagram in java.

Leave a Comment

Your email address will not be published. Required fields are marked *