Skip to content

Instantly share code, notes, and snippets.

@shailrshah
Last active October 4, 2017 00:31
Show Gist options
  • Save shailrshah/c53887d73e205f5943d8532e4e33fa63 to your computer and use it in GitHub Desktop.
Save shailrshah/c53887d73e205f5943d8532e4e33fa63 to your computer and use it in GitHub Desktop.
Return true iff the two given strings have the same character frequencies.
static boolean isAnagram(String a, String b) {
if(a.length() != b.length()) return false;
return Arrays.equals(getCount(a.toLowerCase()), getCount(b.toLowerCase()));
}
static int[] getCount(String str) {
int[] count = new int[26];
str.chars().forEach(ch -> count[ch-'a']++);
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment