Created
July 5, 2016 15:20
-
-
Save qingl97/13b493f08059ae606ad01f6e62805262 to your computer and use it in GitHub Desktop.
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
public class Solution { | |
// only for ASCII characters which takes one byte in storage | |
public boolean isAnagram(String s, String t) { | |
if(s == null && t == null) | |
return true; | |
if(s == null && t != null) | |
return false; | |
if(s != null && t == null) | |
return false; | |
if(s.length() != t.length()) | |
return false; | |
int[] counts1 = new int[256]; | |
int[] counts2 = new int[256]; | |
parse(counts1, s); | |
parse(counts2, t); | |
for(int i=0; i<256; i++) | |
if(counts1[i] != counts2[i]) | |
return false; | |
return true; | |
} | |
private void parse(int[] arr, String s) { | |
for(char c : s.toCharArray()) | |
arr[c] += 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment