diff --git a/solution/3100-3199/3163.String Compression III/README.md b/solution/3100-3199/3163.String Compression III/README.md index 0e3135ffb0678..33d38e003cded 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 { + + +### 方法二:双指针 + + + +#### 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; +} +``` + + + + + + + +### 方法三:正则匹配 + + + +#### 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..e0972d9f11106 --- /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..4d3cb23c5103f --- /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..f1e54e8af2f67 --- /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..a11377e20af03 --- /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; +}