Last active
August 29, 2015 14:08
-
-
Save ColdSauce/59074d3d71d30ef68ef5 to your computer and use it in GitHub Desktop.
interview question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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