-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCompression.js
More file actions
35 lines (28 loc) · 880 Bytes
/
Copy pathStringCompression.js
File metadata and controls
35 lines (28 loc) · 880 Bytes
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
const top = (arr) => arr[arr.length - 1];
const isCompactable = (s1, s2) => s1 !== undefined && s1.indexOf(s2) >= 0;
const stringAdd = (s1, s2) => {
const s1Num = Number(s1.slice(0, s1.indexOf(s2)));
return s1Num === 0 ? `2${s2}` : `${s1Num + 1}${s2}`;
};
const compact = (sliceNumber, s) => {
const stack = [];
while (s.length >= sliceNumber) {
const _s = s.slice(0, sliceNumber);
if (isCompactable(top(stack), _s)) stack.push(stringAdd(stack.pop(), _s));
else stack.push(_s);
s = s.slice(sliceNumber);
}
stack.push(s);
return stack.join("");
};
function solution(s) {
const sliceNumbers = [...Array(parseInt(s.length / 2)).keys()].map(
(n) => n + 1
);
if (s.length === 1) return 1;
return Math.min(
...sliceNumbers
.map((sliceNumber) => compact(sliceNumber, s))
.map((compactedStr) => compactedStr.length)
);
}