From dd1159c7997cd2e498a3d74fa50b1f096d87828d Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 4 Nov 2024 11:38:52 +0300 Subject: [PATCH 1/3] feat: add solutions to lc problem: No.3163 --- .../3163.String Compression III/README.md | 86 +++++++++++++++++++ .../3163.String Compression III/README_EN.md | 86 +++++++++++++++++++ .../3163.String Compression III/Solution2.js | 12 +++ .../3163.String Compression III/Solution2.ts | 12 +++ .../3163.String Compression III/Solution3.js | 11 +++ .../3163.String Compression III/Solution3.ts | 11 +++ 6 files changed, 218 insertions(+) create mode 100644 solution/3100-3199/3163.String Compression III/Solution2.js create mode 100644 solution/3100-3199/3163.String Compression III/Solution2.ts create mode 100644 solution/3100-3199/3163.String Compression III/Solution3.js create mode 100644 solution/3100-3199/3163.String Compression III/Solution3.ts diff --git a/solution/3100-3199/3163.String Compression III/README.md b/solution/3100-3199/3163.String Compression III/README.md index 0e3135ffb0678..d93449d7ddcf9 100644 --- a/solution/3100-3199/3163.String Compression III/README.md +++ b/solution/3100-3199/3163.String Compression III/README.md @@ -213,4 +213,90 @@ function compressedString(word: string): string { + + +### Solution 2: Two Pointers + + + +#### TypeScript + +```ts +function compressedString(word: string): string { + let res = ''; + + for (let i = 1, j = 0; i <= word.length; i++) { + if (word[i] !== word[j] || i - j === 9) { + res += i - j + word[j]; + j = i; + } + } + + return res; +} +``` + +#### JavaScript + +```js +function compressedString(word) { + let res = ''; + + for (let i = 1, j = 0; i <= word.length; i++) { + if (word[i] !== word[j] || i - j === 9) { + res += i - j + word[j]; + j = i; + } + } + + return res; +} +``` + + + + + + + +### Solution 3: RegExp + + + +#### TypeScript + +```ts +function compressedString(word: string): string { + const regex = /(.)\1{0,8}/g; + let m: RegExpMatchArray | null = null; + let res = ''; + + while ((m = regex.exec(word))) { + res += m[0].length + m[1]; + } + + return res; +} +``` + +#### JavaScript + +```js +function compressedString(word) { + const regex = /(.)\1{0,8}/g; + let m = null; + let res = ''; + + while ((m = regex.exec(word))) { + res += m[0].length + m[1]; + } + + return res; +} +``` + + + + + diff --git a/solution/3100-3199/3163.String Compression III/README_EN.md b/solution/3100-3199/3163.String Compression III/README_EN.md index 7c4452240287a..f903d2ca2c447 100644 --- a/solution/3100-3199/3163.String Compression III/README_EN.md +++ b/solution/3100-3199/3163.String Compression III/README_EN.md @@ -209,4 +209,90 @@ function compressedString(word: string): string { + + +### Solution 2: Two Pointers + + + +#### TypeScript + +```ts +function compressedString(word: string): string { + let res = ''; + + for (let i = 1, j = 0; i <= word.length; i++) { + if (word[i] !== word[j] || i - j === 9) { + res += i - j + word[j]; + j = i; + } + } + + return res; +} +``` + +#### JavaScript + +```js +function compressedString(word) { + let res = ''; + + for (let i = 1, j = 0; i <= word.length; i++) { + if (word[i] !== word[j] || i - j === 9) { + res += i - j + word[j]; + j = i; + } + } + + return res; +} +``` + + + + + + + +### Solution 3: RegExp + + + +#### TypeScript + +```ts +function compressedString(word: string): string { + const regex = /(.)\1{0,8}/g; + let m: RegExpMatchArray | null = null; + let res = ''; + + while ((m = regex.exec(word))) { + res += m[0].length + m[1]; + } + + return res; +} +``` + +#### JavaScript + +```js +function compressedString(word) { + const regex = /(.)\1{0,8}/g; + let m = null; + let res = ''; + + while ((m = regex.exec(word))) { + res += m[0].length + m[1]; + } + + return res; +} +``` + + + + + diff --git a/solution/3100-3199/3163.String Compression III/Solution2.js b/solution/3100-3199/3163.String Compression III/Solution2.js new file mode 100644 index 0000000000000..865180eb2ca4f --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution2.js @@ -0,0 +1,12 @@ +function compressedString(word) { + let res = ''; + + for (let i = 1, j = 0; i <= word.length; i++) { + if (word[i] !== word[j] || i - j === 9) { + res += i - j + word[j]; + j = i; + } + } + + return res; +} diff --git a/solution/3100-3199/3163.String Compression III/Solution2.ts b/solution/3100-3199/3163.String Compression III/Solution2.ts new file mode 100644 index 0000000000000..17b56812ee469 --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution2.ts @@ -0,0 +1,12 @@ +function compressedString(word: string): string { + let res = ''; + + for (let i = 1, j = 0; i <= word.length; i++) { + if (word[i] !== word[j] || i - j === 9) { + res += i - j + word[j]; + j = i; + } + } + + return res; +} diff --git a/solution/3100-3199/3163.String Compression III/Solution3.js b/solution/3100-3199/3163.String Compression III/Solution3.js new file mode 100644 index 0000000000000..b11a9d671b5fe --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution3.js @@ -0,0 +1,11 @@ +function compressedString(word) { + const regex = /(.)\1{0,8}/g; + let m = null; + let res = ''; + + while ((m = regex.exec(word))) { + res += m[0].length + m[1]; + } + + return res; +} diff --git a/solution/3100-3199/3163.String Compression III/Solution3.ts b/solution/3100-3199/3163.String Compression III/Solution3.ts new file mode 100644 index 0000000000000..8466dc07715e3 --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution3.ts @@ -0,0 +1,11 @@ +function compressedString(word: string): string { + const regex = /(.)\1{0,8}/g; + let m: RegExpMatchArray | null = null; + let res = ''; + + while ((m = regex.exec(word))) { + res += m[0].length + m[1]; + } + + return res; +} From 9517afdb64829997c0297f35b8181995ed547b73 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 4 Nov 2024 08:39:49 +0000 Subject: [PATCH 2/3] style: format code and docs with prettier --- .../3163.String Compression III/Solution2.js | 24 +++++++++---------- .../3163.String Compression III/Solution2.ts | 24 +++++++++---------- .../3163.String Compression III/Solution3.js | 22 ++++++++--------- .../3163.String Compression III/Solution3.ts | 22 ++++++++--------- 4 files changed, 46 insertions(+), 46 deletions(-) diff --git a/solution/3100-3199/3163.String Compression III/Solution2.js b/solution/3100-3199/3163.String Compression III/Solution2.js index 865180eb2ca4f..e0972d9f11106 100644 --- a/solution/3100-3199/3163.String Compression III/Solution2.js +++ b/solution/3100-3199/3163.String Compression III/Solution2.js @@ -1,12 +1,12 @@ -function compressedString(word) { - let res = ''; - - for (let i = 1, j = 0; i <= word.length; i++) { - if (word[i] !== word[j] || i - j === 9) { - res += i - j + word[j]; - j = i; - } - } - - return res; -} +function compressedString(word) { + let res = ''; + + for (let i = 1, j = 0; i <= word.length; i++) { + if (word[i] !== word[j] || i - j === 9) { + res += i - j + word[j]; + j = i; + } + } + + return res; +} diff --git a/solution/3100-3199/3163.String Compression III/Solution2.ts b/solution/3100-3199/3163.String Compression III/Solution2.ts index 17b56812ee469..4d3cb23c5103f 100644 --- a/solution/3100-3199/3163.String Compression III/Solution2.ts +++ b/solution/3100-3199/3163.String Compression III/Solution2.ts @@ -1,12 +1,12 @@ -function compressedString(word: string): string { - let res = ''; - - for (let i = 1, j = 0; i <= word.length; i++) { - if (word[i] !== word[j] || i - j === 9) { - res += i - j + word[j]; - j = i; - } - } - - return res; -} +function compressedString(word: string): string { + let res = ''; + + for (let i = 1, j = 0; i <= word.length; i++) { + if (word[i] !== word[j] || i - j === 9) { + res += i - j + word[j]; + j = i; + } + } + + return res; +} diff --git a/solution/3100-3199/3163.String Compression III/Solution3.js b/solution/3100-3199/3163.String Compression III/Solution3.js index b11a9d671b5fe..f1e54e8af2f67 100644 --- a/solution/3100-3199/3163.String Compression III/Solution3.js +++ b/solution/3100-3199/3163.String Compression III/Solution3.js @@ -1,11 +1,11 @@ -function compressedString(word) { - const regex = /(.)\1{0,8}/g; - let m = null; - let res = ''; - - while ((m = regex.exec(word))) { - res += m[0].length + m[1]; - } - - return res; -} +function compressedString(word) { + const regex = /(.)\1{0,8}/g; + let m = null; + let res = ''; + + while ((m = regex.exec(word))) { + res += m[0].length + m[1]; + } + + return res; +} diff --git a/solution/3100-3199/3163.String Compression III/Solution3.ts b/solution/3100-3199/3163.String Compression III/Solution3.ts index 8466dc07715e3..a11377e20af03 100644 --- a/solution/3100-3199/3163.String Compression III/Solution3.ts +++ b/solution/3100-3199/3163.String Compression III/Solution3.ts @@ -1,11 +1,11 @@ -function compressedString(word: string): string { - const regex = /(.)\1{0,8}/g; - let m: RegExpMatchArray | null = null; - let res = ''; - - while ((m = regex.exec(word))) { - res += m[0].length + m[1]; - } - - return res; -} +function compressedString(word: string): string { + const regex = /(.)\1{0,8}/g; + let m: RegExpMatchArray | null = null; + let res = ''; + + while ((m = regex.exec(word))) { + res += m[0].length + m[1]; + } + + return res; +} From fca7d48638741a7d36fd382d0022d059c6bbe4a8 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 4 Nov 2024 17:18:42 +0800 Subject: [PATCH 3/3] Update README.md --- solution/3100-3199/3163.String Compression III/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solution/3100-3199/3163.String Compression III/README.md b/solution/3100-3199/3163.String Compression III/README.md index d93449d7ddcf9..33d38e003cded 100644 --- a/solution/3100-3199/3163.String Compression III/README.md +++ b/solution/3100-3199/3163.String Compression III/README.md @@ -215,7 +215,7 @@ function compressedString(word: string): string { -### Solution 2: Two Pointers +### 方法二:双指针 @@ -259,7 +259,7 @@ function compressedString(word) { -### Solution 3: RegExp +### 方法三:正则匹配