Anagrams in java sourcecode

Check whether the strings are Anagrams or not??

 Anagram:-Two words having same character and same number of character but arrangement is different .
Example of anagram:- cat-act,listen-silent,etc 
 

                                        type 1 

logic:-
step1:-See two strings are equal in length or not if equal using length() function after this go to step 2
step2:- See the character of this strings are same or not using charAt() function .
sourcecode:-

package strings;
public class anagram {
    public static void main(String[] args) {
        String A="cat";
        String B="act";
        boolean isAnagram=false;
        boolean visited[]=new boolean[B.length()];
        if(A.length()==B.length()) {      
        for(int i=0; i<A.length(); i++) {
            char C=A.charAt(i);
            isAnagram=false;
            for(int j=0;j<B.length();j++) {
                if(B.charAt(j)==C && !visited[j]) {
                    visited[j]=true;
                    isAnagram=true;
                    break;
                }
            }
            if(!isAnagram) {
                break;
            }
         }
     }  
        if(isAnagram) {
            System.out.println("Yes given string is anagram");
        }
        else {
            System.out.println("No given strings is not anagram");
        }
    }
}
 output:-
Yes given string is anagram
                                                        

                                    type2

sourcecode:-
package strings;
public class anagram {
    public static void main(String[] args) {
        String A="cats";
        String B="act";
        boolean isAnagram=false;
        int al[]=new int[256];
        int bl[]=new int[256];
        for(char c:A.toCharArray()) {
            int index=(int)c;
            al[index]++;
        }
        for(char c:B.toCharArray()) {
            int index=(int)c;
            bl[index]++;
        }
        for(int i=0;i<256;i++) {
            if(al[i]==bl[i]);
            isAnagram=true;
            break;
        }
        if(isAnagram) {
            System.out.println("Yes given string is anagram");
        }
        else {
            System.out.println("No given strings is not anagram");
        }
    }
}
output:-
No given strings is not anagram

         type3:similar to type 2 but more optimization


package strings;
public class anagram {
    public static void main(String[] args) {
        String A="listen";
        String B="silent";
        boolean isAnagram=false;
        int al[]=new int[256];
        for(char c:A.toCharArray()) {
            int index=(int)c;
            al[index]++;
        }
        for(char c:B.toCharArray()) {
            int index=(int)c;
            al[index]--;
        }
        for(int i=0;i<256;i++) {
            if(al[i]==0);
            isAnagram=true;
            break;
        }
        if(isAnagram) {
            System.out.println("Yes given string is anagram");
         }
        else {
            System.out.println("No given strings is not anagram");
        }
    }
}
output:-
Yes given string is anagram

Comments