We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Anagrams
You are viewing a single comment's thread. Return to all comments →
static boolean isAnagram(String a, String b) { // Complete the function