-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
duplicate-encoder.js
80 lines (69 loc) · 2.57 KB
/
duplicate-encoder.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
function duplicateEncode(word){
// a place to store whether or not a letter is a duplicate
const isDuplicate = {};
// iterate over the word
for (let i = 0; i < word.length; i++) {
const letter = word[i].toLowerCase();
// if we have seen this letter before set to true
if (letter in isDuplicate) {
isDuplicate[letter] = true;
} else {
isDuplicate[letter] = false;
}
}
// a place to store the parenthesis
let parenthesis = '';
// iterate over the word
for (let i = 0; i < word.length; i++) {
const letter = word[i].toLowerCase();
// if this letter is a duplicate
if (isDuplicate[letter]) {
// append a )
parenthesis += ')';
} else {
// append a (
parenthesis += '(';
}
}
return parenthesis;
}
function duplicateEncode(word){
const isDuplicate = Array.prototype.reduce.call(word, (isDuplicate, letter) => {
letter = letter.toLowerCase();
if (letter in isDuplicate) {
isDuplicate[letter] = true;
} else {
isDuplicate[letter] = false;
}
return isDuplicate;
}, {});
return Array.prototype.reduce.call(word, (parenthesis, letter) => {
letter = letter.toLowerCase();
if (isDuplicate[letter]) {
parenthesis += ')';
} else {
parenthesis += '(';
}
return parenthesis;
}, '');
}
function duplicateEncode(word){
const isDuplicate = Array.prototype.reduce.call(word, (isDuplicate, letter) => {
letter = letter.toLowerCase();
return (letter in isDuplicate ? isDuplicate[letter] = true : isDuplicate[letter] = false, isDuplicate);
}, {});
return Array.prototype.reduce.call(word, (parenthesis, letter) => {
letter = letter.toLowerCase();
return isDuplicate[letter] ? parenthesis + ')' : parenthesis + '(';
}, '');
}
function duplicateEncode(word){
const isDuplicate = Array.prototype.reduce.call(word, (dup, letter) => (letter.toLowerCase() in dup ? dup[letter.toLowerCase()] = true : dup[letter.toLowerCase()] = false, dup), {});
return Array.prototype.reduce.call(word, (parenthesis, letter) => isDuplicate[letter.toLowerCase()] ? parenthesis + ')' : parenthesis + '(', '');
}
// Courtesy of meiamsome:
const duplicateEncode = cased => [cased.toLowerCase().split('')].map(word => [word.reduce((acc, c) => (acc[c] = (acc[c] || 0) + 1, acc), {})].map(counts => word.map(c => counts[c] > 1 ? ')' : '('))[0].join(''))[0];
console.log(duplicateEncode('din'),'(((');
console.log(duplicateEncode('recede'),'()()()');
console.log(duplicateEncode('Success'),')())())','should ignore case');
console.log(duplicateEncode('(( @'),'))((');