-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0411-minimum-unique-word-abbreviation.js
More file actions
86 lines (78 loc) · 3.72 KB
/
0411-minimum-unique-word-abbreviation.js
File metadata and controls
86 lines (78 loc) · 3.72 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
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
/**
* Minimum Unique Word Abbreviation
* Time Complexity: O(2^N * M * N)
* Space Complexity: O(M * N)
*/
var minAbbreviation = function (target, dictionary) {
const targetLengthValue = target.length;
let minAbbreviationLength = targetLengthValue;
let bestFoundAbbreviation = target;
const filteredDictionaryEntries = dictionary.filter(dictWordItem => dictWordItem.length === targetLengthValue);
for (let currentMaskIteration = 0; currentMaskIteration < (1 << targetLengthValue); currentMaskIteration++) {
const candidateAbbreviationString = constructAbbreviationFromMask(target, currentMaskIteration);
if (candidateAbbreviationString.length > minAbbreviationLength) {
continue;
}
let isCurrentAbbrUnique = true;
for (let dictIndex = 0; dictIndex < filteredDictionaryEntries.length; dictIndex++) {
const currentDictionaryWord = filteredDictionaryEntries[dictIndex];
if (testForConflict(candidateAbbreviationString, currentDictionaryWord)) {
isCurrentAbbrUnique = false;
break;
}
}
if (isCurrentAbbrUnique) {
if (candidateAbbreviationString.length < minAbbreviationLength) {
minAbbreviationLength = candidateAbbreviationString.length;
bestFoundAbbreviation = candidateAbbreviationString;
} else if (candidateAbbreviationString.length === minAbbreviationLength) {
if (candidateAbbreviationString < bestFoundAbbreviation) {
bestFoundAbbreviation = candidateAbbreviationString;
}
}
}
}
return bestFoundAbbreviation;
function constructAbbreviationFromMask(originalTargetString, abbreviationMaskValue) {
let abbreviationResult = '';
let consecutiveSkippedCount = 0;
for (let charPosition = 0; charPosition < originalTargetString.length; charPosition++) {
const bitCheckValue = 1 << charPosition;
if ((abbreviationMaskValue & bitCheckValue) > 0) {
if (consecutiveSkippedCount > 0) {
abbreviationResult += consecutiveSkippedCount;
consecutiveSkippedCount = 0;
}
abbreviationResult += originalTargetString[charPosition];
} else {
consecutiveSkippedCount++;
}
}
if (consecutiveSkippedCount > 0) {
abbreviationResult += consecutiveSkippedCount;
}
return abbreviationResult;
}
function testForConflict(inputAbbreviation, comparisonDictionaryWord) {
let inputAbbrPointer = 0;
let comparisonWordPointer = 0;
while (inputAbbrPointer < inputAbbreviation.length && comparisonWordPointer < comparisonDictionaryWord.length) {
const currentAbbrCharCheck = inputAbbreviation[inputAbbrPointer];
if (isNaN(parseInt(currentAbbrCharCheck))) {
if (currentAbbrCharCheck !== comparisonDictionaryWord[comparisonWordPointer]) {
return false;
}
inputAbbrPointer++;
comparisonWordPointer++;
} else {
let numberValueFromAbbr = 0;
while (inputAbbrPointer < inputAbbreviation.length && !isNaN(parseInt(inputAbbreviation[inputAbbrPointer]))) {
numberValueFromAbbr = numberValueFromAbbr * 10 + parseInt(inputAbbreviation[inputAbbrPointer]);
inputAbbrPointer++;
}
comparisonWordPointer += numberValueFromAbbr;
}
}
return inputAbbrPointer === inputAbbreviation.length && comparisonWordPointer === comparisonDictionaryWord.length;
}
};