You are viewing a single comment's thread. Return to all comments →
static boolean isAnagram(String a, String b) {
//Checking two strings lengths
if(a.length()!=b.length()) { return false; }
2.//Converting the string of characters into Uppercase (we can use Lowercase too)
a=a.toUpperCase(); b=b.toUpperCase();
3.//converting strings into Character Array
char [] ch1=a.toCharArray(); char [] ch2=b.toCharArray();
4.//Sorting two Character Arrays
char temp; for(int i=0;i<=ch1.length-1;i++) { for(int j=i+1;j<=ch1.length-1;j++) { if(ch1[i]>ch1[j]) { temp=ch1[i]; ch1[i]=ch1[j]; ch1[j]=temp; } if(ch2[i]>ch2[j]) { temp=ch2[i]; ch2[i]=ch2[j]; ch2[j]=temp; } } }
//Checking the two Arrays character by character
for(int i=0;i<=ch1.length-1;i++) { if(ch1[i]!=ch2[i]) { return false; } } return true;
}
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) {
//Checking two strings lengths
2.//Converting the string of characters into Uppercase (we can use Lowercase too)
3.//converting strings into Character Array
4.//Sorting two Character Arrays
//Checking the two Arrays character by character
for(int i=0;i<=ch1.length-1;i++) { if(ch1[i]!=ch2[i]) { return false; } } return true;
}