Skip to content

Instantly share code, notes, and snippets.

@omargfh
Last active November 21, 2022 01:23
Show Gist options
  • Save omargfh/2607edd9d787137d8adc14e5632ce56e to your computer and use it in GitHub Desktop.
Save omargfh/2607edd9d787137d8adc14e5632ce56e to your computer and use it in GitHub Desktop.
LeetCode Runtime: 81ms (top 2.45%), Memory: 43.7 MB (top 3%)
const LETTERS: number = 26;
const ASCII_LOWER: number = 'a'.charCodeAt(0);
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) {
return false;
}
let s_a: number[] = [];
let t_a: number[] = [];
for (let i = 0; i < LETTERS; i++) {
s_a.push(0);
t_a.push(0);
}
for (let i = 0, n = s.length; i < n; i++) {
s_a[s.charCodeAt(i) - ASCII_LOWER]++;
t_a[t.charCodeAt(i) - ASCII_LOWER]++;
}
for (let i = 0; i < LETTERS; i++) {
if (s_a[i] !== t_a[i]) {
return false;
}
}
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment