How to Check two String are Anagram or not in Java

Connect with

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

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

To Check Anagram using for loop


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();
}


By 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;
}

By 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);
}

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);
	}
}

Output of Program

true
true
true
false

Suggestions are welcome to improve this post.


Connect with

Leave a Reply

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