-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path242_valid_anagram.ts
More file actions
34 lines (32 loc) · 991 Bytes
/
242_valid_anagram.ts
File metadata and controls
34 lines (32 loc) · 991 Bytes
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
function isAnagram(s: string, t: string): boolean {
if(s.length != t.length) return false
const sList = s.split("");
const sSet = new Set(sList);
const tList = t.split("");
const tSet = new Set(tList);
const arr1 = Array.from(sSet).sort();
const arr2 = Array.from(tSet).sort();
if(arr1.some((value, index) => value !== arr2[index])) return false;
const sLetterCount = {}
const tLetterCount = {}
for(let i=0; i<sList.length; i++){
if(sList[i] in sLetterCount){
sLetterCount[sList[i]]=sLetterCount[sList[i]]+1
} else {
sLetterCount[sList[i]]=1
}
}
for(let i=0; i<tList.length; i++){
if(tList[i] in tLetterCount){
tLetterCount[tList[i]]=tLetterCount[tList[i]]+1
} else {
tLetterCount[tList[i]]=1
}
}
for(const k in sLetterCount){
if(tLetterCount[k]!=sLetterCount[k]){
return false
}
}
return true;
};