-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid-anagram.ts
69 lines (55 loc) · 1.32 KB
/
valid-anagram.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Given two strings s and t, return true if t is an
anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging
the letters of a different word or phrase,
typically using all the original letters exactly once.
Constraints:
1 <= s.length, t.length <= 5 * 104
s and t consist of lowercase English letters.
Follow up: What if the inputs contain Unicode characters?
How would you adapt your solution to such a case?
*/
const EXAMPLES = [
{
input: {
s: 'anagram',
t: 'nagaram',
},
output: true,
},
{
input: {
s: 'rat',
t: 'car',
},
output: false,
},
];
/** Time O(n) | Space O(n) */
function isAnagramValid({ s, t }: { s: string; t: string }) {
if (s.length !== t.length) {
return false;
}
const charCount: Record<string, number> = {};
for (let i = 0; i < s.length; i++) {
const char = s[i];
charCount[char] = charCount[char] ? charCount[char] + 1 : 1;
}
for (let i = 0; i < t.length; i++) {
const char = t[i];
if (!charCount[char]) {
return false;
}
charCount[char]--;
}
return true;
}
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest;
it('Valid Anagram', () => {
EXAMPLES.forEach((example) => {
expect(isAnagramValid(example.input)).toEqual(example.output);
});
});
}