-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram.js
54 lines (41 loc) · 1.09 KB
/
anagram.js
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
// given two strings - write a loop that measures if string1 is an anagram of string2.
export function anagram(str1, str2){
// check if the lengths of the strings are the same.
if (str1.length !== str2.length){
return false;
}
let str1Counter = {};
let str2Counter = {};
for (let val of str1){
str1Counter[val] = (str1Counter[val] || 0) + 1;
}
for (let letter of str2){
str2Counter[letter] = (str2Counter[letter] || 0) + 1;
}
for (let key in str1Counter){
if (!(key in str2Counter)){
return false;
}
if (str2Counter[key] !== str1Counter[key]){
return false;
}
}
return true;
}
export function anagramRefactor(str1, str2){
if (str1.length !== str2.length){
return false;
}
let check = {};
for (let letter in str1){
check[letter] = (str1[letter] || 0) + 1;
}
for (let letter in str2){
if (!(letter in check)){
return false;
} else {
check[letter] -= 1;
}
}
return true;
}