-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringMinimizer.js
61 lines (55 loc) · 1.76 KB
/
stringMinimizer.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
/*
Challenge: String minimizer
Solution complexity:
Time: O(n)
Space: O(n)
*/
// Helper functions
const isIdentifier = char => (/[a-zA-Z]/).test(char);
const isKnownWord = (word, obj) => obj.hasOwnProperty(word);
const getIdentifierValue = (identifier, knownWordsObj, wordIndex) => {
if (isKnownWord(identifier, knownWordsObj)) {
return knownWordsObj[identifier];
} else {
knownWordsObj[identifier] = '$' + wordIndex;
return identifier;
}
}
/*
Accumulate alphabetical characters into an identifier until a non-identifier is reached, then concatenate the identifier or its related value (e.g., $0) as well as the current non-identifier to the minimized string.
*/
const minimize = str => {
let minimizedStr = '';
let identifier = '';
let wordIndex = 0;
const knownWordsObj = {};
for (let i = 0; i < str.length; i++) {
if (isIdentifier(str[i])) {
identifier += str[i];
} else {
if (identifier) {
minimizedStr += getIdentifierValue(identifier, knownWordsObj, wordIndex);
wordIndex++;
}
minimizedStr += str[i];
identifier = '';
}
}
// If identifier isn't empty at end of loop, update minimized string one last time
if (identifier) {
minimizedStr += getIdentifierValue(identifier, knownWordsObj, wordIndex);
}
return minimizedStr;
}
// Input strings
const str1 = '';
const str2 = 'you say yes, I say no you say stop and I say go go go';
const str3 = 'you say yes, I say alice jump4joy you say stop and I say go go go alice jump4joy';
console.log('String 1:', str1);
console.log('Minimized:', minimize(str1));
console.log();
console.log('String 2:', str2);
console.log('Minimized:', minimize(str2));
console.log();
console.log('String 3:', str3);
console.log('Minimized:', minimize(str3));