-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0320-generalized-abbreviation.js
More file actions
37 lines (32 loc) · 1.22 KB
/
0320-generalized-abbreviation.js
File metadata and controls
37 lines (32 loc) · 1.22 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
/**
* Generalized Abbreviation
* Time Complexity: O(N * 2^N)
* Space Complexity: O(N * 2^N)
*/
var generateAbbreviations = function (word) {
const abbreviationList = [];
const stringLength = word.length;
function exploreCombinations(currentAbbreviation, characterPointer, consecutiveCount) {
if (characterPointer === stringLength) {
let finalAbbreviation = currentAbbreviation;
if (consecutiveCount > 0) {
finalAbbreviation += consecutiveCount;
}
abbreviationList.push(finalAbbreviation);
return;
}
const nextCharacterPointerForAbbr = characterPointer + 1;
const nextConsecutiveCountForAbbr = consecutiveCount + 1;
exploreCombinations(currentAbbreviation, nextCharacterPointerForAbbr, nextConsecutiveCountForAbbr);
let updatedAbbreviationForm = currentAbbreviation;
if (consecutiveCount > 0) {
updatedAbbreviationForm += consecutiveCount;
}
updatedAbbreviationForm += word[characterPointer];
const nextCharacterPointerForInclusion = characterPointer + 1;
const resetAbbrCount = 0;
exploreCombinations(updatedAbbreviationForm, nextCharacterPointerForInclusion, resetAbbrCount);
}
exploreCombinations('', 0, 0);
return abbreviationList;
};