Saturday, 21 January 2017

Write a Java Program to Check weather two Strings are Anagram or Not ?

For interview Perspective it is one of the most important question to check weather two string are Anagram or not.
Anagram means two string must have same characters but in different sequence. For example suppose we have one String "THE EYES" and other String is "THEY SEE" ,these Strings are Anagram because both strings have same set of characters but in different sequence. For more example see below program .

Anagram Rules:
  • Both strings must have same length.
  • Convert both strings in uppercase or lowercase.
  • Remove all the white spaces from both strings.
  • Both strings must have same characters in any sequence. 
  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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
import java.util.Map;
import java.util.Arrays;
import java.util.LinkedHashMap;

public class CheckStringAnagram {
 
 //First Way to Verify Anagram using - String class contains() method
 public static void isAnagram_1(String firstStr,String secondStr){
  String first = firstStr.replaceAll("\\s", "").toLowerCase();
  String second = secondStr.replaceAll("\\s", "").toLowerCase();
  boolean status = false;
  
  if(first.length() != second.length()){
   status = false;
  }
  else{
   for(int i=0;i<first.length();i++){
    if(second.contains(String.valueOf(first.charAt(i)))){
     status = true;
    }
    else{
     status = false;
    }
   }
  }
  if(status){
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Anagram.");
  }
  else{
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Not Anagram.");
  }
 }
 //Second way to Verify Anagram using - java.util.Arrays; methods sort() and equals()
 public static void isAnagram_2(String firstStr,String secondStr){
  String first = firstStr.replaceAll("\\s", "").toLowerCase();
  String second = secondStr.replaceAll("\\s", "").toLowerCase();
  boolean status = false;
  
  if(first.length() != second.length()){
   status = false;
  }
  else{
   char[] firstArr = first.toCharArray();
   char[] secondArr = second.toCharArray();
   
   Arrays.sort(firstArr);
   Arrays.sort(secondArr);
   
   status = Arrays.equals(firstArr,secondArr);
  }
  if(status){
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Anagram.");
  }
  else{
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Not Anagram.");
  }
 }
 //Third way to Verify Anagram using - Map interface
 public static void isAnagram_3(String firstStr,String secondStr){
  String first = firstStr.replaceAll("\\s", "").toLowerCase();
  String second = secondStr.replaceAll("\\s", "").toLowerCase();
  boolean status = false;
  
  if(first.length() != second.length()){
   status = false;
  }
  else{
   Map<Character, Integer> map = new LinkedHashMap<>();
   for(int i=0;i<first.length();i++){
    if(map.containsKey(first.charAt(i))){
     map.put(first.charAt(i), map.get(first.charAt(i))+1);
    }
    else{
     map.put(first.charAt(i), 1);
    }
   }
   for(int i=0;i<second.length();i++){
    if(map.containsKey(second.charAt(i))){
     map.put(second.charAt(i), map.get(second.charAt(i))-1);
    }
    else{
     map.put(second.charAt(i), 1);
    }
   }
   for(int i : map.values()){
    if(i != 0) status = false;
    else status = true;
   }
  }
  if(status){
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Anagram.");
  }
  else{
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Not Anagram.");
  }
 }
 public static void main(String[] args) {
  CheckStringAnagram.isAnagram_1("violence", "nice");
  CheckStringAnagram.isAnagram_1("MIRACLE", "CLAIMER");
  CheckStringAnagram.isAnagram_3("THE EYES", "THEY SEE");
  CheckStringAnagram.isAnagram_1("MOTHER IN LAW", "WOMAN HITLER");
  CheckStringAnagram.isAnagram_3("School Master", "The Classroom");
  CheckStringAnagram.isAnagram_1("THAT QUEER SHAKE", "THE EARTHQUAKES");
  CheckStringAnagram.isAnagram_2("ANGELS AND DEMONS", "aaddeeglmnnnoss");
  CheckStringAnagram.isAnagram_2("Goldens and Names", "Endless God Manna");
 }
}


Output:

*violence* and *nice* are Not Anagram.
*MIRACLE* and *CLAIMER* are Anagram.
*THE EYES* and *THEY SEE* are Anagram.
*MOTHER IN LAW* and *WOMAN HITLER* are Anagram.
*School Master* and *The Classroom* are Anagram.
*THAT QUEER SHAKE* and *THE EARTHQUAKES* are Anagram.
*ANGELS AND DEMONS* and *aaddeeglmnnnoss* are Anagram.
*Goldens and Names* and *Endless God Manna* are Anagram.




      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment