Java Anagrams

  • + 0 comments

    static boolean isAnagram(String a, String b) { // Complete the function

        //To convert the String into charArray
        char[] aChar=a.toLowerCase().toCharArray();
        char[] bChar=b.toLowerCase().toCharArray();
    
        //To note the alaphabets frequency a to z
        int [] aInt=new int[26];
        int [] bInt=new int[26];
    
        byte counter=0;
    
        //Evaluting the frequency of a to z in the char Arrays
        for(char i='a';i<='z';i++){
            for(int j=0;j<aChar.length;j++){
                if(aChar[j]==i){
                    aInt[counter]+=1;
                }
            }
            for(int j=0;j<bChar.length;j++){
                if(bChar[j]==i){
                    bInt[counter]+=1;
                }
            }
            counter++;
        }
    
        //Comparing the both array aInt & bInt
    
        boolean flag=false;
        for(int m=0;m<26;m++){
            if(aInt[m]!=bInt[m]){
                return false;
            }else{
                flag=true;
            }
        }
        return flag;
    
    }