Skip to content

Commit

Permalink
1. valid-anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
whewchews committed Aug 23, 2024
1 parent 81d4e12 commit 33af494
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions valid-anagram/whewchews.ts
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 33af494

Please sign in to comment.