Skip to content

Commit

Permalink
valid anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
Totschka committed Dec 16, 2024
1 parent 026e627 commit 225af48
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions valid-anagram/Totschka.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// https://leetcode.com/problems/valid-anagram/

/**
* @SC `O(N)`
* @TC `O(1)`
*/
namespace use_hashmap {
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) {
return false;
}
const counter = {};
for (const char of s) {
counter[char] = (counter[char] || 0) + 1;
}
for (const char of t) {
if (!counter[char]) {
return false;
}
counter[char] = counter[char] - 1;
}
return true;
}
}

/**
* @SC `O(log(N)^2)`
* @TC `O((Nlog(N))^2)`
*/
namespace navie_approach {
function isAnagram(s: string, t: string): boolean {
return [...s].sort().join('') === [...t].sort().join('');
}
}

0 comments on commit 225af48

Please sign in to comment.