How to check whether two given Strings are Anagram or not in Java?
Problem Description :
Write a program in java that checks whether two Strings are Anagram of each other or not.
![]() |
Strings are Anagram of each other or not? |
Concept :
Two Strings are Anagram of each other if both Strings contains the same set of characters and length is also same. Here we are using the containsAll() method of List Interface, this method returns true if elements of two lists are same.
Java Program :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.string; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category String Interview Questions | |
* @problem Check whether two Strings are anagram of each other or not. | |
* | |
*/ | |
public class Anagram { | |
public static void main(String[] args) { | |
Scanner input = null; | |
try { | |
input = new Scanner(System.in); | |
System.out.println("Enter first string: "); | |
String firstString = input.next(); | |
System.out.println("Enter second string: "); | |
String secondString = input.next(); | |
int firstStringLength = firstString.length(); | |
int secondStringLength = secondString.length(); | |
List<Character> firstList = new ArrayList<>(); | |
List<Character> secondList = new ArrayList<>(); | |
// Converting First String to List. | |
for (int i = 0; i < firstStringLength; i++) { | |
firstList.add(firstString.charAt(i)); | |
} | |
// Converting Second String to List. | |
for (int i = 0; i < secondStringLength; i++) { | |
secondList.add(secondString.charAt(i)); | |
} | |
if (isAnagram(firstList, secondList)) { | |
System.out.println("Anagram"); | |
} else { | |
System.out.println("Not Anagram"); | |
} | |
} finally { | |
if (input != null) { | |
input.close(); | |
} | |
} | |
} | |
private static boolean isAnagram(List<Character> firstList, | |
List<Character> secondList) { | |
int firstListSize = firstList.size(); | |
int secondListSize = secondList.size(); | |
boolean result = false; | |
if (firstListSize == secondListSize) { | |
if (firstList.containsAll(secondList) && secondList.containsAll(firstList)) { | |
result = true; | |
} | |
} | |
return result; | |
} | |
} |
Output :
Case 1 :
![]() |
Output - Given Strings are Anagram of each other. |
Case 2 :
![]() |
Output - Given Strings are not Anagram of each other. |
References :
Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.
Tags : Interview Questions on String, Solution in Java, Anagram, List, ArrayList, for loop, if statement.
Tags : Interview Questions on String, Solution in Java, Anagram, List, ArrayList, for loop, if statement.
How to check whether two given Strings are Anagram or not in Java?
Reviewed by Rohit Agarwal
on
11/08/2016
Rating:

No comments:
Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.