How to find whether 2 strings are anagram or not using java

2 strings are anagrams, if one string is a jumbled form of other string. In technical terms, we can say like, every characters in the first string should appear int the second string and the count of each character should be the same. Eg:- "ajva" is an anagram of "java". But "avaja" is not an anagram of "java" because the latter contains more number of "a".

There are many methods to find out whether 2 strings are anagrams or not. One of the method is to sort both the strings and check whether they are equal. Minimum time complexity taken in this method is   O(n log n).

The other method is to store the count of each of the characters of the strings in 2 separate maps. And then check the count of each characters. This method will use some extra storage spaces, but the time taken is in O(n).

The program is as follows:


import java.util.HashMap;
import java.util.Map;


public class Anagram {
public static void main(String args[])
{
String s1 = "sujithmo";
String s2 = "julomith";
if(isAnagram(s1,s2))
{
System.out.println(" Both the strings are anagram");
}
else
{
System.out.println(" Strings are not anagram");
}
}

public static boolean isAnagram(String s1,String s2)
{
if(s1.length()!=s2.length())
{
return false;
}
Map<Character,Integer> map1= new HashMap<Character, Integer>();
Map<Character,Integer> map2= new HashMap<Character, Integer>();
//taking the count value of each element in the 1st string
for(int i=0;i<s1.length();i++)
{
if(map1.containsKey(s1.charAt(i)))
{
int getnum= map1.get(s1.charAt(i));
map1.put(s1.charAt(i), (getnum+1));
}
else
{
map1.put(s1.charAt(i),1);
}
}
//taking the count value of each element in the 2nd string
for(int i=0;i<s2.length();i++)
{
if(map2.containsKey(s2.charAt(i)))
{
int getnum= map2.get(s2.charAt(i));
map2.put(s2.charAt(i), (getnum+1));
}
else
{
map2.put(s2.charAt(i),1);
}
}
if(map1.size()!=map2.size())
{
return false;
}
//iterating the first map, and checking whether count of each key is equal to count of 
//corresponding key in the second map
for(Map.Entry<Character,Integer> entry: map1.entrySet() )
{
char key = entry.getKey();
if(entry.getValue()!=map2.get(key))
{
return false;
}
}
return true;
}
}



The output of the program is as follows: 


 Strings are not anagram

Similar programs: 


I hope this posts have given you the right idea about anagram check. Please comment below if you have any doubts. Every doubt will be responded within a day. Suggestions are most welcome.

Comments

  1. An anagram is a type of word, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.

    For example: orchestra can be rearranged into carthorse or cat can be rearranged into act.

    We can find out the anagram strings using below algorithm:

    public static boolean isAnagram(String str1, String str2) {
    if (str1 == null || str2 == null) {
    return false;
    } else if (str1.length() != str2.length()) {
    return false;
    }

    Map map = new HashMap();

    for (int i = 0; i < str1.length(); i++) {
    char characters = str1.charAt(i);
    int charInStr1 = map.containsKey(characters) ? map.get(characters) : 0;
    map.put(characters, ++charInStr1);
    char charFromStr2 = str2.charAt(i);
    int charsInRight = map.containsKey(charFromStr2) ? map.get(charFromStr2) : 0;
    map.put(charFromStr2, --charsInRight);
    }

    for (int occurrences : map.values()) {
    if (occurrences != 0) {
    return false;
    }
    }
    return true;
    }

    I found below useful links for more information

    Write program to find if Strings are anagram

    ReplyDelete

Post a Comment

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++