From 1e41aa89a0284563e844ed7daf257a2f369a854f Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 10 Dec 2024 15:36:24 +0300 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.2981 --- .../README.md | 82 +++++++++++++++++++ .../README_EN.md | 82 +++++++++++++++++++ .../Solution2.js | 30 +++++++ .../Solution2.ts | 30 +++++++ 4 files changed, 224 insertions(+) create mode 100644 solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/Solution2.js create mode 100644 solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I/Solution2.ts 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..54ef41ccd2f66 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 { + + +### 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/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; +} From 50ef20788555673ab7528720726e56de2de8fbe8 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 11 Dec 2024 18:44:05 +0800 Subject: [PATCH 2/2] Update README.md --- .../README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 54ef41ccd2f66..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 @@ -271,9 +271,9 @@ function maximumLength(s: string): number { -### Solution 2: Counting +### 方法二:计数 -The time complexity is $O(n)$ +时间复杂度 $O(n)$。