Given two strings, write a code to check whether two strings are anagram of each other or not. In this tutorial, I am going to discuss multiple approaches and their java implementation to check if two strings are anagrams or not.
Let’s first understand what is an anagram? and how we are going to solve this problem.
What is an Anagram?
Two strings are said to be anagrams of each other if it contains the same characters, only the order of characters in both the strings is different. In other words, both strings must contain the same exact letters in the same exact frequency.
Let’s understand this through an example –
For example –
i)
str1 – car, str2 – rac
In this example, str1 and str2 are anagrams of each other. As both, the strings contain the same letters only the order of characters in both the strings is different.
ii)
str1 – code, str2 – dock
In this example, str1 and str2 are not an anagram of each other. As both, the strings contain different letters.
Now we know what’s an Anagram. Let’s think for a moment how do we solve this problem. What’s the approach we are going to use.
How to Check If Two Strings are Anagram of each other
Method 1 – Use Sorting
The easiest approach is to sort both the strings and after sorting compare them. If sorted strings are equal then it’s an anagram otherwise it’s not.
The time complexity of this approach is O(nlogn).
Sorting algorithms and their time complexity
Java Program to Check Anagram using Sorting
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//Check anagram using sorting import java.util.*; class Anagram { public static void main (String[] args) { Scanner in = new Scanner(System.in); //String input String str1 = in.nextLine(); String str2 = in.nextLine(); //Forming char array char arr1[] = str1.toCharArray(); char arr2[] = str2.toCharArray(); //Sort character array Arrays.sort(arr1); Arrays.sort(arr2); String sortedstr1 = new String(arr1); String sortedstr2 = new String(arr2); if(sortedstr1.equals(sortedstr2)) { System.out.println("Anagram"); } else { System.out.println("Not an Anagram"); } } |
Programming questions on Linked List
METHOD 2- Check if Character Count is Same in Both the Strings
In this method, we count each character of the first string then subtracting it from the count of the second string. Finally, we check if the character count is zero. If it is not zero(0) then the two string is not an anagram else it’s an anagram.
The time complexity of this approach is O(n).
Check whether two strings are anagram of each other video tutorial
Check whether Two Strings are Anagram of each other in Java
In this code example, we are going to implement method 2. In which we check if character count is the same in both the strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import java.util.*; class Anagram { public static boolean isAnagram(String str1, String str2) { /*If both strings is of different length, then it's not an anagram */ if(str1.length() != str2.length()) return false; //Create an array of size 256 int[] countarr = new int[256]; for(int i = 0; i < str1.length(); i++) { //Increment character count for str1 countarr[str1.charAt(i)]++; //decrement character count for str2 countarr[str2.charAt(i)]--; } for(int j = 0; j < countarr.length; j++) { //if it's not zero if( countarr[j] != 0) { return false; } } return true; } public static void main (String[] args) { Scanner in = new Scanner(System.in); //String input String str1 = in.nextLine(); String str2 = in.nextLine(); if(isAnagram(str1, str2)) { System.out.println("Anagram"); } else { System.out.println("Not an Anagram"); } } } |