-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0471-encode-string-with-shortest-length.js
More file actions
54 lines (46 loc) · 2.28 KB
/
0471-encode-string-with-shortest-length.js
File metadata and controls
54 lines (46 loc) · 2.28 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
/**
* Encode String With Shortest Length
* Time Complexity: O(N^3)
* Space Complexity: O(N^3)
*/
var encode = function (s) {
const stringLength = s.length;
const dpStore = Array.from({ length: stringLength }, () => new Array(stringLength).fill(''));
for (let subLength = 1; subLength <= stringLength; subLength++) {
for (let startIdx = 0; startIdx <= stringLength - subLength; startIdx++) {
const endIdx = startIdx + subLength - 1;
const currentSubstr = s.slice(startIdx, endIdx + 1);
dpStore[startIdx][endIdx] = currentSubstr;
for (let partitionIdx = startIdx; partitionIdx < endIdx; partitionIdx++) {
const leftPart = dpStore[startIdx][partitionIdx];
const rightPart = dpStore[partitionIdx + 1][endIdx];
const combinedPart = leftPart + rightPart;
if (combinedPart.length < dpStore[startIdx][endIdx].length) {
dpStore[startIdx][endIdx] = combinedPart;
}
}
for (let subpatternLen = 1; subpatternLen < subLength; subpatternLen++) {
if (subLength % subpatternLen === 0) {
const foundPattern = s.slice(startIdx, startIdx + subpatternLen);
const repeatCount = subLength / subpatternLen;
let isRepeatable = true;
for (let patternSegment = 1; patternSegment < repeatCount; patternSegment++) {
const nextSegmentStart = startIdx + patternSegment * subpatternLen;
const nextSegmentEnd = nextSegmentStart + subpatternLen;
if (s.slice(nextSegmentStart, nextSegmentEnd) !== foundPattern) {
isRepeatable = false;
break;
}
}
if (isRepeatable) {
const encodedVersion = `${repeatCount}[${dpStore[startIdx][startIdx + subpatternLen - 1]}]`;
if (encodedVersion.length < dpStore[startIdx][endIdx].length) {
dpStore[startIdx][endIdx] = encodedVersion;
}
}
}
}
}
}
return dpStore[0][stringLength - 1];
};