diff --git a/valid-anagram/whewchews.ts b/valid-anagram/whewchews.ts new file mode 100644 index 000000000..72b630d7d --- /dev/null +++ b/valid-anagram/whewchews.ts @@ -0,0 +1,21 @@ +function isAnagram(s: string, t: string): boolean { + // TC: O(1) + if (s.length !== t.length) return false; + + // SC: O(N) + const count: { [key: string]: number } = {}; + + // TC: O(N) + for (let i = 0; i <= s.length - 1; i++) { + const sChar = s[i]; + const tChar = t[i]; + count[sChar] = (count[sChar] || 0) + 1; + count[tChar] = (count[tChar] || 0) - 1; + } + + // TC: O(N) + return Object.values(count).every((v) => v === 0); +} + +// TC: O(N) +// SC: O(N)