Skip to content

Instantly share code, notes, and snippets.

@ColdSauce
Last active August 29, 2015 14:08
Show Gist options
  • Save ColdSauce/59074d3d71d30ef68ef5 to your computer and use it in GitHub Desktop.
Save ColdSauce/59074d3d71d30ef68ef5 to your computer and use it in GitHub Desktop.
interview question
boolean areAnagrams(String s1, String s2){
if(s1 == null || s2 == null){
return false;
}
if(s1.isEmpty() || s2.isEmpty()){
return false;
}
if(s1.length() != s2.length()){
return false;
}
int[] alpha = new int[26];
for(int i = 0; i < s1.length(); i++){
alpha[s1.charAt(i) - 'a']++;
if((--alpha[s2.charAt(i)-'a']) < 0){
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment