diff --git a/valid-anagram/jjipper.ts b/valid-anagram/jjipper.ts new file mode 100644 index 000000000..d29b3f618 --- /dev/null +++ b/valid-anagram/jjipper.ts @@ -0,0 +1,28 @@ +/** + * https://leetcode.com/problems/valid-anagram/ + * time complexity : O(n) + * space complexity : O(n) + */ + +function isAnagram(s: string, t: string): boolean { + const count: Record = {}; + if (s.length !== t.length) return false; + + for (let char of s) { + if (count[char] === undefined) { + count[char] = 1; + } else { + count[char] += 1; + } + } + + for (let char of t) { + if (!count[char]) { + return false; + } else { + count[char] -= 1; + } + } + + return Object.values(count).every(v => v === 0); +};