Last active
August 29, 2015 14:02
-
-
Save k1xme/6dfa93c2cc873f6f76e4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess you didn't test your code...