Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions solution/3100-3199/3163.String Compression III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,90 @@ function compressedString(word: string): string {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:双指针

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法三:正则匹配

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
86 changes: 86 additions & 0 deletions solution/3100-3199/3163.String Compression III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,90 @@ function compressedString(word: string): string {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Two Pointers

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 3: RegExp

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
12 changes: 12 additions & 0 deletions solution/3100-3199/3163.String Compression III/Solution2.js
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions solution/3100-3199/3163.String Compression III/Solution2.ts
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions solution/3100-3199/3163.String Compression III/Solution3.js
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions solution/3100-3199/3163.String Compression III/Solution3.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading