Anagram String

What is Anagram String ?

Two strings are said to be anagram if they contain the same characters even though order of characters may be different. For example read and dare,eat and tea,fear and fare and so on

Program 1 : Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using inbuilt Sorting)

import java.util.Arrays;

public class AnagramProgram1 {

	// Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using inbuilt Sorting)
	
	static boolean checkAnagram(char[] charArray1, char[] charArray2)
	{
		int n1 = charArray1.length;
		int n2 = charArray2.length;
		
		if (n1 != n2)
			return false;
		
		Arrays.sort(charArray1);
		Arrays.sort(charArray2);

		for (int i = 0; i < n1; i++)
			if (charArray1[i] != charArray2[i])
				return false;			

		return true;
	}

	public static void main(String args[])
	{
		String str1 = "secure";
		String str2 = "rescue";
		char charArr1[] = str1.toCharArray();
                char charArr2[] = str2.toCharArray();
        
                String str3 = "equals";
		String str4 = "aquire";
		char charArr3[] = str3.toCharArray();
                char charArr4[] = str4.toCharArray();

                if (checkAnagram(charArr1, charArr2))
			System.out.print("The two strings " + str1 + " and " + str2 + " are anagram string");
		else
			System.out.print("The two strings " + str1 + " and " + str2 + " are not string");
                
        
                if (checkAnagram(charArr3, charArr4))
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are anagram string");
		else
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are not anagram string");
	}
}
Output :

The two strings secure and rescue are anagram string
The two strings equals and aquire are not anagram string

Program 2 : Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using inbuilt Sorting And Equals)

import java.util.Arrays;

public class AnagramProgram2 {

	// Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using inbuilt Sorting And Equals)
	
	static boolean checkAnagram(char[] charArray1, char[] charArray2)
	{
		int n1 = charArray1.length;
		int n2 = charArray2.length;
		
		if (n1 != n2)
			return false;
		
		Arrays.sort(charArray1);
		Arrays.sort(charArray2);
			
		 //Comparing both the arrays using in-built function equals ()  
                if(Arrays.equals(charArray1, charArray2) == true) {  
                        return true;
		}  
                else {  
                        return false;
	        }  	

	}

	public static void main(String args[])
	{
		String str1 = "secure";
		String str2 = "rescue";
		char charArr1[] = str1.toCharArray();
                char charArr2[] = str2.toCharArray();
        
                String str3 = "equals";
		String str4 = "aquire";
		char charArr3[] = str3.toCharArray();
                char charArr4[] = str4.toCharArray();

                if (checkAnagram(charArr1, charArr2))
			System.out.print("The two strings " + str1 + " and " + str2 + " are anagram string");
		else
			System.out.print("The two strings " + str1 + " and " + str2 + " are not string");
                
        
                if (checkAnagram(charArr3, charArr4))
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are anagram string");
		else
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are not anagram string");
	}

}
Output :

The two strings secure and rescue are anagram string
The two strings equals and aquire are not anagram string

Program 3 : Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using Characters Count)

import java.util.Arrays;

public class AnagramProgram3 {

	// Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using Characters Count)
	
	static int TOTAL_CHARS = 256;

	static boolean checkAnagram(char str1[], char str2[])
	{
		int count1[] = new int[TOTAL_CHARS];
		Arrays.fill(count1, 0);
		int count2[] = new int[TOTAL_CHARS];
		Arrays.fill(count2, 0);

		int i;

		for (i = 0; i < str1.length && i < str2.length;i++) {
			count1[str1[i]]++;
			count2[str2[i]]++;
		}

		if (str1.length != str2.length)
			return false;

		for (i = 0; i < TOTAL_CHARS; i++)
			if (count1[i] != count2[i])
				return false;

		return true;
	}

	public static void main(String args[])
	{
		String str1 = "secure";
		String str2 = "rescue";
		char charArr1[] = str1.toCharArray();
                char charArr2[] = str2.toCharArray();
        
                String str3 = "equals";
		String str4 = "aquire";
		char charArr3[] = str3.toCharArray();
                char charArr4[] = str4.toCharArray();

                if (checkAnagram(charArr1, charArr2))
			System.out.print("The two strings " + str1 + " and " + str2 + " are anagram string");
		else
			System.out.print("The two strings " + str1 + " and " + str2 + " are not string");
                
        
                if (checkAnagram(charArr3, charArr4))
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are anagram string");
		else
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are not anagram string");
	}
}
Output :

The two strings secure and rescue are anagram string
The two strings equals and aquire are not anagram string

Program 4 : Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using One Array For Characters Count)

public class AnagramProgram4 {

	// Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using One Array For Characters Count)

	static int TOTAL_CHARS = 256;

	static boolean checkAnagram(char[] str1, char[] str2)
	{
		int[] count = new int[TOTAL_CHARS];
		int i;

		for (i = 0; i < str1.length; i++) {
			count[str1[i] - 'a']++;
			count[str2[i] - 'a']--;
		}

		if (str1.length != str2.length)
			return false;

		for (i = 0; i < TOTAL_CHARS; i++)
			if (count[i] != 0) {
				return false;
		}
		
		return true;
	}

	public static void main(String[] args)
	{
		String str1 = "secure";
		String str2 = "rescue";
		char charArr1[] = str1.toCharArray();
                char charArr2[] = str2.toCharArray();
        
                String str3 = "equals";
		String str4 = "aquire";
		char charArr3[] = str3.toCharArray();
                char charArr4[] = str4.toCharArray();

                if (checkAnagram(charArr1, charArr2))
			System.out.print("The two strings " + str1 + " and " + str2 + " are anagram string");
		else
			System.out.print("The two strings " + str1 + " and " + str2 + " are not string");
                
        
                if (checkAnagram(charArr3, charArr4))
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are anagram string");
		else
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are not anagram string");
	}	
}
Output :

The two strings secure and rescue are anagram string
The two strings equals and aquire are not anagram string

Program 5 : Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using HashMap())

import java.util.HashMap;
import java.util.Set;

public class AnagramProgram5 {

	// Java Program To Check Whether Two Strings Are Two Strings Are Anagram (Using One Array For Characters Count)

	public static boolean checkAnagram(String a, String b)
	{
		if (a.length() != b.length()) {
			return false;
		}

		HashMap<Character, Integer> map = new HashMap<>();

		for (int i = 0; i < a.length(); i++) {
			if (map.containsKey(a.charAt(i))) {
				map.put(a.charAt(i),
						map.get(a.charAt(i)) + 1);
			}
			else {
				map.put(a.charAt(i), 1);
			}
		}

		for (int i = 0; i < b.length(); i++) {

			if (map.containsKey(b.charAt(i))) {				
				map.put(b.charAt(i),
						map.get(b.charAt(i)) - 1);
			}
		}

		Set<Character> keys = map.keySet();
	
		for (Character key : keys) {
			if (map.get(key) != 0) {
				return false;
			}
		}
		return true;
	}
	public static void main(String[] args)
	{
		String str1 = "secure";
		String str2 = "rescue";		
                String str3 = "equals";
		String str4 = "aquire";
		

                if (checkAnagram(str1, str2))
			System.out.print("The two strings " + str1 + " and " + str2 + " are anagram string");
		else
			System.out.print("The two strings " + str1 + " and " + str2 + " are not string");
                
        
                if (checkAnagram(str3, str4))
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are anagram string");
		else
			System.out.print("\nThe two strings " + str3 + " and " + str4 + " are not anagram string");
	}

}
Output :

The two strings secure and rescue are anagram string
The two strings equals and aquire are not anagram string
Scroll to Top