Skip to content

Instantly share code, notes, and snippets.

@qingl97
Created July 5, 2016 15:20
Show Gist options
  • Save qingl97/13b493f08059ae606ad01f6e62805262 to your computer and use it in GitHub Desktop.
Save qingl97/13b493f08059ae606ad01f6e62805262 to your computer and use it in GitHub Desktop.
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