-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisAnagram.js
156 lines (136 loc) · 4.51 KB
/
isAnagram.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Check if two string are anagrams
// Count letters function
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 count object
if (!counts[letter]) {
// add it with 0
counts[letter] = 0;
}
// increment the count of the current letter in the counts object
counts[letter]++;
}
// return counts object
return counts;
}
// Count letters function, reduce
function countLetters(input) {
return [...input.toLowerCase()].reduce((counts, letter) => {
counts[letter] = counts[letter] || 0;
counts[letter]++;
return counts
}, {})
}
// Count letters function, reduce (one liner), just for learning purposes
function countLetters(input) {
return [...input.toLowerCase()].reduce((counts, letter) => {
// the comma operator executes multiple statements
// the last statement is the thing the exression evaluates to
return(counts[letter] = counts[letter] || 0, counts[letter]++, counts)
}, {})
}
// console.log(countLetters("apple"));
function isAnagram(test, original) {
// if length is not the same, instant false
if (test.length !== original.length) {
return false;
}
// if the words are exactly equal, instant true
if (test === original) {
return true;
}
// count letters in test, store in variable
const testCount = countLetters(test);
// count letters in test, store in variable
const originalCount = countLetters(original);
// console.log(testCounts);
// console.log(originalCounts);
const testCountKeys = Object.keys(testCount);
console.log(testCountKeys);
// check if both counts have the same number of keys
if (testCountKeys.length !== Object.keys(originalCount).length) {
return false;
}
// Faster alternative for code below
return testCountKeys.every(letter => {
return testCount[letter] === originalCount[letter]
});
// iterate over the keys of the first counts object
for (let i = 0; i < testCountKeys.length; i++) {
const letter = testCountKeys[i];
// if the second counts object does not have the same count for the current key,
if (testCount[letter] !== originalCount[letter]) {
return false;
}
}
return true;
}
console.log(isAnagram("foefet", "toffee"));
console.log(isAnagram("Buckethead", "DeathCubeK"));
console.log(isAnagram("Twoo", "WooT"));
console.log(isAnagram("dumle", "bumble"));
const { array } = require("prop-types");
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 count object
if (!counts[letter]) {
// add it with 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) => {
return(counts[letter] = counts[letter] || 0, counts[letter]++, counts)
}, {})
}
// console.log(countLetters("apple"));
function isAnagram(test, original) {
// if length is not the same, instant false
if (test.length !== original.length) {
return false;
}
// if the words are exactly equal, instant true
if (test === original) {
return true;
}
// count letters in test, store in variable
const testCount = countLetters(test);
// count letters in test, store in variable
const originalCount = countLetters(original);
// console.log(testCounts);
// console.log(originalCounts);
const testCountKeys = Object.keys(testCount);
console.log(testCountKeys);
// check if both counts have the same number of keys
if (testCountKeys.length !== Object.keys(originalCount).length) {
return false;
}
// iterate over the keys of the first counts object
for (let i = 0; i < testCountKeys.length; i++) {
const letter = testCountKeys[i];
// if the second counts object does not have the same count for the current key,
if (testCount[letter] !== originalCount[letter]) {
return false;
}
}
return true;
}
console.log(isAnagram("foefet", "toffee"));
// console.log(isAnagram("Buckethead", "DeathCubeK"));
console.log(isAnagram("Twoo", "WooT"));
console.log(isAnagram("dumle", "bumble"));