How to Check two String are Anagram or not in Java

Connect with

How to Check two String are Anagram or not in JavaHow to Check two String are Anagram or not in Java. Check if two string are anagram in Java. using for loop, using while loop, using stringBuilder, using array etc.

1. Overview of Anagram

First of all, let us understand what is anagram. if two strings having the same character in a same or different order it means those words are anagram. There are different ways to find whether two strings are anagram or not. What are those way, let us explain one by one by using the program. This is one of the important questions which is being asked by Interview frequently to check the logic.

2. Step to check Anagram

– check the length of strings is equal, if not then not anagram string.
– if same char in both the string in any position, it means anagram.

3. To Check Anagram using for loop

Java program to check if two strings are anagram in Java.

public static boolean isAnagram(String str1, String str2) {
	if (str1.length() != str2.length()) {
		return false;
	}

	char[] chars = str1.toCharArray();

	for (char c : chars) {
	  int index = str2.indexOf(c);
	  if (index != -1) {
	    str2 = str2.substring(0, index) + str2.substring(index + 1, str2.length());
	  } else {
	   return false;
	  }
	}

	return str2.isEmpty();
}


4. Anagram check using StringBuilder class

public static boolean isAnagramUsingSB(String first, String second) {
	char[] characters = first.toCharArray();
	StringBuilder sbSecond = new StringBuilder(second);

	for (char ch : characters) {
		int index = sbSecond.indexOf("" + ch);
		if (index != -1) {
			sbSecond.deleteCharAt(index);
		} else {
			return false;
		}
	}

	return sbSecond.length() == 0 ? true : false;
}

5. Anagram check using Arrays class

public static boolean isAnagramByUsingArrays(String str1, String str2) {

	char[] charFromStr1 = str1.toCharArray();
	char[] charFromStr2 = str2.toCharArray();
	Arrays.sort(charFromStr1);
	Arrays.sort(charFromStr2);

	return Arrays.equals(charFromStr1, charFromStr2);
}

6. Java program to check Anagram Strings

This is complete working program to check Anagram strings in java , using different approaches as follows:

package com.mysoftkey.dsa;

import java.util.Arrays;

/**
 * @author ranjeet.jha
 *
 */
public class AnagramCheckExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println(isAnagram("word", "wdro"));
		System.out.println(isAnagramByUsingArrays("cab", "abc"));
		System.out.println(isAnagramUsingSB("word", "wdro"));

		System.out.println(isAnagramByUsingArrays("cabb", "abc"));

	}

	/*
	 * This method assumes both arguments are not null and in lowercase.
	 *
	 * @return true, if both String are anagram
	 */
	public static boolean isAnagram(String str1, String str2) {
		if (str1.length() != str2.length()) {
			return false;
		}

		char[] chars = str1.toCharArray();

		for (char c : chars) {
			int index = str2.indexOf(c);
			if (index != -1) {
				str2 = str2.substring(0, index) + str2.substring(index + 1, str2.length());
			} else {
				return false;
			}
		}

		return str2.isEmpty();
	}

	/**
	 * In this method help StringBuilder to check whether two string are anagram
	 * or not.
	 * 
	 * @param first
	 * @param second
	 * @return
	 */
	public static boolean isAnagramUsingSB(String first, String second) {
		char[] characters = first.toCharArray();
		StringBuilder sbSecond = new StringBuilder(second);

		for (char ch : characters) {
			int index = sbSecond.indexOf("" + ch);
			if (index != -1) {
				sbSecond.deleteCharAt(index);
			} else {
				return false;
			}
		}

		return sbSecond.length() == 0 ? true : false;
	}

	/*
	 * This java method to check two string are anagram or not by using library.
	 * input of both should be in either lower case only or upper case only.
	 * 
	 * @return true, if both Strings are anagram.
	 */
	public static boolean isAnagramByUsingArrays(String str1, String str2) {

		char[] charFromStr1 = str1.toCharArray();
		char[] charFromStr2 = str2.toCharArray();
		Arrays.sort(charFromStr1);
		Arrays.sort(charFromStr2);

		return Arrays.equals(charFromStr1, charFromStr2);
	}
}

7. Output of program to check 2 strings anagram

true
true
true
false

8. Reference

I hope you enjoyed this post of How to check 2 strings Anagram in Java, and you can visit data structure tutorial for more details.

Visit Wiki page for Anagram check.
Suggestions or comments are welcome to improve this post. cheers
Suggestions are welcome to improve this post. Happy Learning!


Connect with

Leave a Comment

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