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
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,86 @@ function maximumLength(s: string): number {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:计数

时间复杂度 $O(n)$。

<!-- tabs:start -->

#### TypeScript

```ts
function maximumLength(s: string): number {
const cnt = new Map<string, number>();
const n = s.length;
let [c, ch] = [0, ''];

for (let i = 0; i < n + 1; i++) {
if (ch === s[i]) {
c++;
} else {
let j = 1;
while (c) {
const char = ch.repeat(j++);
cnt.set(char, (cnt.get(char) ?? 0) + c);
c--;
}

ch = s[i];
c = 1;
}
}

let res = -1;
for (const [x, c] of cnt) {
if (c >= 3) {
res = Math.max(res, x.length);
}
}

return res;
}
```

### JavaScript

```js
function maximumLength(s) {
const cnt = new Map();
const n = s.length;
let [c, ch] = [0, ''];

for (let i = 0; i < n + 1; i++) {
if (ch === s[i]) {
c++;
} else {
let j = 1;
while (c) {
const char = ch.repeat(j++);
cnt.set(char, (cnt.get(char) ?? 0) + c);
c--;
}

ch = s[i];
c = 1;
}
}

let res = -1;
for (const [x, c] of cnt) {
if (c >= 3) {
res = Math.max(res, x.length);
}
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,86 @@ function maximumLength(s: string): number {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Counting

The time complexity is $O(n)$

<!-- tabs:start -->

#### TypeScript

```ts
function maximumLength(s: string): number {
const cnt = new Map<string, number>();
const n = s.length;
let [c, ch] = [0, ''];

for (let i = 0; i < n + 1; i++) {
if (ch === s[i]) {
c++;
} else {
let j = 1;
while (c) {
const char = ch.repeat(j++);
cnt.set(char, (cnt.get(char) ?? 0) + c);
c--;
}

ch = s[i];
c = 1;
}
}

let res = -1;
for (const [x, c] of cnt) {
if (c >= 3) {
res = Math.max(res, x.length);
}
}

return res;
}
```

### JavaScript

```js
function maximumLength(s) {
const cnt = new Map();
const n = s.length;
let [c, ch] = [0, ''];

for (let i = 0; i < n + 1; i++) {
if (ch === s[i]) {
c++;
} else {
let j = 1;
while (c) {
const char = ch.repeat(j++);
cnt.set(char, (cnt.get(char) ?? 0) + c);
c--;
}

ch = s[i];
c = 1;
}
}

let res = -1;
for (const [x, c] of cnt) {
if (c >= 3) {
res = Math.max(res, x.length);
}
}

return res;
}
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function maximumLength(s) {
const cnt = new Map();
const n = s.length;
let [c, ch] = [0, ''];

for (let i = 0; i < n + 1; i++) {
if (ch === s[i]) {
c++;
} else {
let j = 1;
while (c) {
const char = ch.repeat(j++);
cnt.set(char, (cnt.get(char) ?? 0) + c);
c--;
}

ch = s[i];
c = 1;
}
}

let res = -1;
for (const [x, c] of cnt) {
if (c >= 3) {
res = Math.max(res, x.length);
}
}

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function maximumLength(s: string): number {
const cnt = new Map<string, number>();
const n = s.length;
let [c, ch] = [0, ''];

for (let i = 0; i < n + 1; i++) {
if (ch === s[i]) {
c++;
} else {
let j = 1;
while (c) {
const char = ch.repeat(j++);
cnt.set(char, (cnt.get(char) ?? 0) + c);
c--;
}

ch = s[i];
c = 1;
}
}

let res = -1;
for (const [x, c] of cnt) {
if (c >= 3) {
res = Math.max(res, x.length);
}
}

return res;
}
Loading