Vasily Guzov 1 year ago
parent 120eccaa4b
commit 0ae89af862

@ -1,34 +1,3 @@
// export function compress(chars: string[]) {
// let L = 0;
// let R = 1;
// const N = chars.length;
// let result = "";
// let currentCounter = 1;
// while (L <= N && R <= N) {
// const curL = chars[L];
// const curR = chars[R];
// if (curL !== curR || curR === undefined) {
// result += curL;
// if (currentCounter > 1) {
// result += currentCounter;
// currentCounter = 1;
// }
// L = R;
// }
// if (curL === curR) {
// currentCounter++;
// }
// R++
// }
// return result.length;
// };
export function compress(chars: string[]) { export function compress(chars: string[]) {
let L = 0; let L = 0;
let R = 1; let R = 1;
@ -39,7 +8,6 @@ export function compress(chars: string[]) {
const curL = chars[L]; const curL = chars[L];
const curR = chars[R]; const curR = chars[R];
if (curL !== curR || curR === undefined) { if (curL !== curR || curR === undefined) {
if (currentCounter > 1) { if (currentCounter > 1) {
[...("" + currentCounter)].forEach((item, i) => { [...("" + currentCounter)].forEach((item, i) => {
@ -55,16 +23,14 @@ export function compress(chars: string[]) {
chars[R] = " "; chars[R] = " ";
} }
R++ R++;
} }
for (let [key, value] of Object.entries(chars)) { for (let i = chars.length - 1; i >= 0; i--) {
if (value === "") { if (chars[i] === " ") {
chars.splice(+key, 1) chars.splice(i, 1);
} }
} }
console.log({ chars }) return chars.length;
}
return chars.filter(item => item).length;
};

Loading…
Cancel
Save