From 0ae89af86280c2e5d7918e945f75a79794f834a5 Mon Sep 17 00:00:00 2001 From: Vasily Guzov Date: Thu, 14 Nov 2024 21:43:18 +0300 Subject: [PATCH] [HAI] my solution finish https://leetcode.com/problems/string-compression/submissions/1451041094/ --- lib/compress/compress.ts | 48 ++++++---------------------------------- 1 file changed, 7 insertions(+), 41 deletions(-) diff --git a/lib/compress/compress.ts b/lib/compress/compress.ts index 2ee8190..6393a21 100644 --- a/lib/compress/compress.ts +++ b/lib/compress/compress.ts @@ -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[]) { let L = 0; let R = 1; @@ -39,7 +8,6 @@ export function compress(chars: string[]) { const curL = chars[L]; const curR = chars[R]; - if (curL !== curR || curR === undefined) { if (currentCounter > 1) { [...("" + currentCounter)].forEach((item, i) => { @@ -52,19 +20,17 @@ export function compress(chars: string[]) { if (curL === curR) { currentCounter++; - chars[R] = ""; + chars[R] = " "; } - R++ + R++; } - for (let [key, value] of Object.entries(chars)) { - if (value === "") { - chars.splice(+key, 1) + for (let i = chars.length - 1; i >= 0; i--) { + if (chars[i] === " ") { + chars.splice(i, 1); } } - console.log({ chars }) - - return chars.filter(item => item).length; -}; + return chars.length; +}