http://www.technicalpage.net/search/label/SQL

Test Anagram in Java

 

package test;

 

import java.util.ArrayList;

import java.util.Arrays;

 

public class Anagram {

 

       public static void main(String[] args) {

             //Anagram are the strings which might contain different number of words, different order of the characters but they are composed of same chars , same number of each respective chars, the order might be different , might be upper or lower case but same chars.

             String str1 = "John o is Good";

             String str2 = "joshi Nodgoo";

             String str3 = str1.replace(" ", "");

             String str4 = str2.replace(" ", "");

             String str5=str3.toLowerCase();

             String str6=str4.toLowerCase();        

            

             String[] strArr1 = new String[str5.length()];

             String[] strArr2 = new String[str6.length()];

             for(int i=0;i<str5.length();i++) {

                    strArr1[i] = Character.toString(  str5.charAt(i)   );

             }

             Arrays.sort(strArr1);

             System.out.println("First Array"+Arrays.toString(strArr1));

            

             for(int i=0;i<str6.length();i++) {

                    strArr2[i] = Character.toString(  str6.charAt(i)   );

             }

             Arrays.sort(strArr2);

             System.out.println("Second Array"+Arrays.toString(strArr2));

            

             if(Arrays.equals(strArr1, strArr2)) {

                    System.out.println("Are they Anagram, as per Arrays ? True");

             } else {

                    System.out.println("Are they Anagram, as per Arrays ? False");

             }

            

             //OR use arraylist after sorting the arrays

             System.out.println("-------------------------ArrayList---------------------------------");

             ArrayList<String> alist = new ArrayList<>();

             ArrayList<String> blist = new ArrayList<>();

             for(int i=0;i<str5.length();i++) {

                    alist.add(strArr1[i]);

             }

             System.out.println("alist: "+alist);

            

             for(int i=0;i<str6.length();i++) {

                    blist.add(strArr2[i]);

             }

             System.out.println("blist: "+blist);

            

             if(alist.equals(blist)) {

                    System.out.println("Are they Anagram, as per ArrayLists ? True");

             } else {

                    System.out.println("Are they Anagram, as per ArrayLists ? False");

             }

            

 

       }

 

}

 

 

Output:

First Array[d, g, h, i, j, n, o, o, o, o, s]

Second Array[d, g, h, i, j, n, o, o, o, o, s]

Are they Anagram, as per Arrays ? True

-------------------------ArrayList---------------------------------

alist: [d, g, h, i, j, n, o, o, o, o, s]

blist: [d, g, h, i, j, n, o, o, o, o, s]

Are they Anagram, as per ArrayLists ? True

No comments:

Post a Comment