Skip to content

Commit e343f20

Browse files
committed
valid anagram
1 parent 026e627 commit e343f20

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

valid-anagram/Totschka.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// https://leetcode.com/problems/valid-anagram/
2+
3+
/**
4+
* @SC `O(N)`
5+
* @TC `O(N)`
6+
*/
7+
namespace use_hashmap {
8+
function isAnagram(s: string, t: string): boolean {
9+
if (s.length !== t.length) {
10+
return false;
11+
}
12+
const counter = {};
13+
for (const char of s) {
14+
counter[char] = (counter[char] || 0) + 1;
15+
}
16+
for (const char of t) {
17+
if (!counter[char]) {
18+
return false;
19+
}
20+
counter[char] = counter[char] - 1;
21+
}
22+
return true;
23+
}
24+
}
25+
26+
/**
27+
* @SC `O(N)`
28+
* @TC `O(Nlog(N))`
29+
*/
30+
namespace naive_approach {
31+
function isAnagram(s: string, t: string): boolean {
32+
return [...s].sort().join('') === [...t].sort().join('');
33+
}
34+
}

0 commit comments

Comments
 (0)