diff --git a/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/README.md b/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/README.md index 1803db0911635..0dc0b392f3669 100644 --- a/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/README.md +++ b/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/README.md @@ -269,4 +269,86 @@ function maximumLength(s: string): number { + + +### 方法二:计数 + +时间复杂度 $O(n)$。 + + + +#### TypeScript + +```ts +function maximumLength(s: string): number { + 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; +} +``` + +### 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; +} +``` + + + + + diff --git a/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/README_EN.md b/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/README_EN.md index ad15033df3ed7..db29f33cd8612 100644 --- a/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/README_EN.md +++ b/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/README_EN.md @@ -267,4 +267,86 @@ function maximumLength(s: string): number { + + +### Solution 2: Counting + +The time complexity is $O(n)$ + + + +#### TypeScript + +```ts +function maximumLength(s: string): number { + 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; +} +``` + +### 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; +} +``` + + + + + diff --git a/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/Solution2.js b/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/Solution2.js new file mode 100644 index 0000000000000..4f4777a20a6ba --- /dev/null +++ b/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/Solution2.js @@ -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; +} diff --git a/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/Solution2.ts b/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/Solution2.ts new file mode 100644 index 0000000000000..e35a981a8734b --- /dev/null +++ b/solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/Solution2.ts @@ -0,0 +1,30 @@ +function maximumLength(s: string): number { + 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; +}