Skip to content

Instantly share code, notes, and snippets.

@k1xme
Last active August 29, 2015 14:02
Show Gist options
  • Save k1xme/6dfa93c2cc873f6f76e4 to your computer and use it in GitHub Desktop.
Save k1xme/6dfa93c2cc873f6f76e4 to your computer and use it in GitHub Desktop.
class Solution{
public static boolean isPermutation(String s1, String s2){
if(s1 === null || s2 == null ) return false;
if(s1.length() != s2.length()) return false;
if(s1.length() == 0) return true; // If s1.length() == s2.length() and s1 is a empty string, s2 is the permutation of s1.
char[] sc1 = s1.toCharArray();
char[] sc2 = s2.toCharArray();
HashMap<String, int> temp = new HashMap<String, boolean>();
for(char c: sc1) {
if(!temp.containsKey(c))
temp.put(c, 1);
else{
int count = temp.get(c) +1;
temp.put(c, count);
};
}
for(char c: sc2) {
if(!temp.containsKey(c))
return false;
else{
int count = temp.get(c) - 1;
if(count<0) return false;
else temp.put(c, count);
};
}
return true;
}
}
@chouclee
Copy link

I guess you didn't test your code...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment