How to check if two Strings are Anagrams in Java

In this post, we will see how to check if two Strings are Anagrams in java.

Anagrams means if two String have same characters but in different order.
For example:  Angel and Angel are anagrams

There are many ways to check if Strings are anagrams. Some of them are:

  1. Using String methods
  2. Using array.sort

Using String methods:

Approach :
  1. If index of any character is -1, then two strings are not anagrams
  2. If index of character is not equal to -1, then remove the character from the String.
  3. If you get empty String in the end, then two Strings are anagrams of each other.

When you run above program, you will get below output:

Using Array.sort

When you run above program, you will get below output:

Other String Programs:

Add Comment