Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Eazybright/a14bcb2996af1585ec94bf80fc825c27 to your computer and use it in GitHub Desktop.
Save Eazybright/a14bcb2996af1585ec94bf80fc825c27 to your computer and use it in GitHub Desktop.
Hackerrank Java Anagrams Solution
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
// // once you declare a.toUppercase you should assign it to a. you cannot define it as just a.toUppercase...
// //I solved it with the long way however I could put a and b in a character array and then use Arrays.sort(arrayname). after this steps convert them to string and check if they are equel.
a=a.toUpperCase();
b=b.toUpperCase();
boolean ret = false;
StringBuilder c= new StringBuilder(b);
if(a.length()==b.length()){
for(int i=0; i<a.length();i++){
for(int j=0; j<c.length();j++){
if(a.charAt(i)==c.charAt(j)){
c.deleteCharAt(j);
if(i==a.length()-1 && c.length()==0){
ret=true;
break;
}
break;
}
}
}
}return ret;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment