You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
677 B
TypeScript

export function compress(chars: string[]) {
let L = 0;
let R = 1;
const N = chars.length;
let currentCounter = 1;
while (L <= N && R <= N) {
const curL = chars[L];
const curR = chars[R];
if (curL !== curR || curR === undefined) {
if (currentCounter > 1) {
[...("" + currentCounter)].forEach((item, i) => {
chars[L + 1 + i] = item;
});
currentCounter = 1;
}
L = R;
}
if (curL === curR) {
currentCounter++;
chars[R] = " ";
}
R++;
}
for (let i = chars.length - 1; i >= 0; i--) {
if (chars[i] === " ") {
chars.splice(i, 1);
}
}
return chars.length;
}