-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagrams.js
More file actions
41 lines (32 loc) · 1.25 KB
/
anagrams.js
File metadata and controls
41 lines (32 loc) · 1.25 KB
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
function solution1(input) {
// stringA = input[0].toLowerCase().split('').filter(l => RegExp(/\w/).test(l)).sort().join('');
// stringB = input[1].toLowerCase().split('').filter(l => RegExp(/\w/).test(l)).sort().join('');
stringA = input[0].replace(/[^\w]/g, "").toLowerCase().split('').sort().join('');
stringB = input[1].replace(/[^\w]/g, "").toLowerCase().split('').sort().join('');
return stringA === stringB;
}
function solution2(input) {
stringA = input[0].replace(/[^\w]/g, "").toLowerCase();
stringB = input[1].replace(/[^\w]/g, "").toLowerCase();
if (stringA.length !== stringB.length) return false;
const hashA = {}, hashB = {};
for(let i = 0; i < stringA.length; i++) {
hashA[stringA[i]] = hashA[stringA[i]] + 1 || 1;
hashB[stringB[i]] = hashB[stringB[i]] + 1 || 1;
}
for(const char in hashA) {
if (hashB[char] !== hashA[char]) return false;
}
return true;
}
function run() {
const inputs = [["HI there", "the hire"], ["hi THERE!", "bye there"], ["hello", "hellos"]];
const solutionsCount = 2;
for (let s = 0; s < solutionsCount; s++) {
for (let i = 0; i < inputs.length; i++) {
const answer = eval(`solution${s + 1}(${JSON.stringify(inputs[i])})`);
console.log(`solution${s + 1} : ${inputs[i]} => ${answer}`);
}
}
}
run();