-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathcompress_go2.ts
More file actions
28 lines (20 loc) · 809 Bytes
/
Copy pathcompress_go2.ts
File metadata and controls
28 lines (20 loc) · 809 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
const assert = require('assert');
const groupByChar = (chars: string[], prev: string | null = null, acc: string[][] = []): string[][] => {
if (chars.length === 0) {
throw new Error(`You silly person, don't compress empty strings :D`);
}
const [ch1, ...chRest] = chars;
if (acc.length === 0 && chRest.length === 0) {
return [[ch1]];
}
if (acc.length === 0) {
return groupByChar(chRest, ch1, [[ch1]]);
}
const newAcc = ch1 === prev ? [...acc.slice(0, -1), [...acc.slice(-1)[0], ch1]]: [...acc, [ch1]]
return chRest.length === 0 ? newAcc : groupByChar(chRest, ch1, newAcc);
}
const compress = (input: string) => groupByChar([...input]).map((v) => `${v.length}${v[0]}`).join('')
assert.equal(
compress("AAABBAAC"),
"3A2B2A1C"
);