-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
anagram-detection.js
129 lines (107 loc) · 3.48 KB
/
anagram-detection.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// AndrewLaneX!
// const countLetters = input => [...input.toLowerCase()].reduce((letters, letter) => ({ ...letters, [letter]: (letters[letter] || 0) + 1 }), {});
function countLetters(input) {
input = input.toLowerCase();
// a place to store the counts
const counts = {};
// iterate over the letters of the word
for (let i = 0; i < input.length; i++) {
const letter = input[i];
// if the letter is not in the counts object
if (!counts[letter]) {
// add it with count 0
counts[letter] = 0;
}
// increment the count of the current letter in the counts object
counts[letter]++;
}
// return counts object
return counts;
}
function countLetters(input) {
return [...input.toLowerCase()].reduce((counts, letter) => {
counts[letter] = counts[letter] || 0;
counts[letter]++;
return counts;
}, {});
}
function countLetters(input) {
return [...input.toLowerCase()].reduce((counts, letter) => {
return (counts[letter] = counts[letter] || 0, counts[letter]++, counts);
}, {});
}
function countLetters(input) {
return [...input.toLowerCase()].reduce((counts, letter) => {
return (counts[letter] = (counts[letter] || 0) + 1, counts);
}, {});
}
function countLetters(input) {
return [...input.toLowerCase()].reduce((counts, letter) => {
// this might not work...
return (counts[letter] = counts[letter]++ || 1, counts);
}, {});
}
function countLetters(input) {
return [...input.toLowerCase()].reduce((counts, letter) => (counts[letter] = (counts[letter] || 0) + 1, counts), {});
}
function countLetters(input) {
return [...input.toLowerCase()].reduce((counts, letter) => {
return {
...counts,
[letter]: (counts[letter] || 0) + 1
};
}, {});
}
function isAnagram(first, second) {
// if length is not the same, instant false
if (first.length !== second.length) {
return false;
}
// if the words are exactly equal, instant true
if (first === second) {
return true;
}
// count letters in first, store in a variable
const firstCounts = countLetters(first);
// count letters in second, store in a variable
const secondCounts = countLetters(second);
const firstCountKeys = Object.keys(firstCounts);
// check if both counts have the same number of keys
if (firstCountKeys.length !== Object.keys(secondCounts).length) {
// if not, instant false
return false;
}
// iterate over the keys of the first counts object
for (let i = 0; i < firstCountKeys.length; i++) {
const letter = firstCountKeys[i];
// if the second counts object does not have the same count for the current key,
if (firstCounts[letter] !== secondCounts[letter]) {
return false;
}
}
// return true
return true;
}
function isAnagram(first, second) {
if (first.length !== second.length) {
return false;
}
if (first === second) {
return true;
}
const firstCounts = countLetters(first);
const secondCounts = countLetters(second);
const firstCountKeys = Object.keys(firstCounts);
if (firstCountKeys.length !== Object.keys(secondCounts).length) {
return false;
}
return firstCountKeys.every(letter => {
return firstCounts[letter] === secondCounts[letter];
});
}
console.log(isAnagram('foefet', 'toffee'), true);
console.log(isAnagram('Buckethead', 'DeathCubeK'), true);
console.log(isAnagram(`Twoo`, 'WooT'), true);
console.log(isAnagram('dumble', 'bumble'), false);
console.log(isAnagram('ound', 'round'), false);
console.log(isAnagram('apple', 'pale'), false);