From 63d3da7b8243a007de0ebf1ab4bbcf4fd01c64a3 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 22 Nov 2023 19:21:57 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1933 No.1933.Check if String Is Decomposable Into Value-Equal Substrings --- .../README.md | 55 +++++++++++++++-- .../README_EN.md | 61 +++++++++++++++++-- .../Solution.cpp | 34 ++++++----- .../Solution.ts | 18 ++++++ 4 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution.ts diff --git a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README.md b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README.md index d48ceda73de2d..1f9d17d12be2f 100644 --- a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README.md +++ b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README.md @@ -61,7 +61,7 @@ 遍历结束后,判断是否出现过长度为 $2$ 的子字符串,若没有,返回 `false`,否则返回 `true`。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。 @@ -87,6 +87,20 @@ class Solution: return cnt2 == 1 ``` +```python +class Solution: + def isDecomposable(self, s: str) -> bool: + cnt2 = 0 + for _, g in groupby(s): + m = len(list(g)) + if m % 3 == 1: + return False + cnt2 += m % 3 == 2 + if cnt2 > 1: + return False + return cnt2 == 1 +``` + ### **Java** @@ -120,13 +134,19 @@ class Solution { class Solution { public: bool isDecomposable(string s) { - int i = 0, n = s.size(); int cnt2 = 0; - while (i < n) { + for (int i = 0, n = s.size(); i < n;) { int j = i; - while (j < n && s[j] == s[i]) ++j; - if ((j - i) % 3 == 1) return false; - if ((j - i) % 3 == 2 && ++cnt2 > 1) return false; + while (j < n && s[j] == s[i]) { + ++j; + } + if ((j - i) % 3 == 1) { + return false; + } + cnt2 += (j - i) % 3 == 2; + if (cnt2 > 1) { + return false; + } i = j; } return cnt2 == 1; @@ -160,6 +180,29 @@ func isDecomposable(s string) bool { } ``` +### **TypeScript** + +```ts +function isDecomposable(s: string): boolean { + const n = s.length; + let cnt2 = 0; + for (let i = 0; i < n; ) { + let j = i; + while (j < n && s[j] === s[i]) { + ++j; + } + if ((j - i) % 3 === 1) { + return false; + } + if ((j - i) % 3 === 2 && ++cnt2 > 1) { + return false; + } + i = j; + } + return cnt2 === 1; +} +``` + ### **...** ``` diff --git a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README_EN.md b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README_EN.md index 410416d65025f..463da18783da9 100644 --- a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README_EN.md +++ b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README_EN.md @@ -52,6 +52,14 @@ ## Solutions +**Solution 1: Two Pointers** + +We traverse the string $s$, using two pointers $i$ and $j$ to count the length of each equal substring. If the length modulo $3$ is $1$, it means that the length of this substring does not meet the requirements, so we return `false`. If the length modulo $3$ is $2$, it means that a substring of length $2$ has appeared. If a substring of length $2$ has appeared before, return `false`, otherwise assign the value of $j$ to $i$ and continue to traverse. + +After the traversal, check whether a substring of length $2$ has appeared. If not, return `false`, otherwise return `true`. + +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. + ### **Python3** @@ -74,6 +82,20 @@ class Solution: return cnt2 == 1 ``` +```python +class Solution: + def isDecomposable(self, s: str) -> bool: + cnt2 = 0 + for _, g in groupby(s): + m = len(list(g)) + if m % 3 == 1: + return False + cnt2 += m % 3 == 2 + if cnt2 > 1: + return False + return cnt2 == 1 +``` + ### **Java** ```java @@ -105,13 +127,19 @@ class Solution { class Solution { public: bool isDecomposable(string s) { - int i = 0, n = s.size(); int cnt2 = 0; - while (i < n) { + for (int i = 0, n = s.size(); i < n;) { int j = i; - while (j < n && s[j] == s[i]) ++j; - if ((j - i) % 3 == 1) return false; - if ((j - i) % 3 == 2 && ++cnt2 > 1) return false; + while (j < n && s[j] == s[i]) { + ++j; + } + if ((j - i) % 3 == 1) { + return false; + } + cnt2 += (j - i) % 3 == 2; + if (cnt2 > 1) { + return false; + } i = j; } return cnt2 == 1; @@ -145,6 +173,29 @@ func isDecomposable(s string) bool { } ``` +### **TypeScript** + +```ts +function isDecomposable(s: string): boolean { + const n = s.length; + let cnt2 = 0; + for (let i = 0; i < n; ) { + let j = i; + while (j < n && s[j] === s[i]) { + ++j; + } + if ((j - i) % 3 === 1) { + return false; + } + if ((j - i) % 3 === 2 && ++cnt2 > 1) { + return false; + } + i = j; + } + return cnt2 === 1; +} +``` + ### **...** ``` diff --git a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution.cpp b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution.cpp index a36d706241461..9f160c786fe02 100644 --- a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution.cpp +++ b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution.cpp @@ -1,15 +1,21 @@ -class Solution { -public: - bool isDecomposable(string s) { - int i = 0, n = s.size(); - int cnt2 = 0; - while (i < n) { - int j = i; - while (j < n && s[j] == s[i]) ++j; - if ((j - i) % 3 == 1) return false; - if ((j - i) % 3 == 2 && ++cnt2 > 1) return false; - i = j; - } - return cnt2 == 1; - } +class Solution { +public: + bool isDecomposable(string s) { + int cnt2 = 0; + for (int i = 0, n = s.size(); i < n;) { + int j = i; + while (j < n && s[j] == s[i]) { + ++j; + } + if ((j - i) % 3 == 1) { + return false; + } + cnt2 += (j - i) % 3 == 2; + if (cnt2 > 1) { + return false; + } + i = j; + } + return cnt2 == 1; + } }; \ No newline at end of file diff --git a/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution.ts b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution.ts new file mode 100644 index 0000000000000..126b5670d3a1a --- /dev/null +++ b/solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/Solution.ts @@ -0,0 +1,18 @@ +function isDecomposable(s: string): boolean { + const n = s.length; + let cnt2 = 0; + for (let i = 0; i < n; ) { + let j = i; + while (j < n && s[j] === s[i]) { + ++j; + } + if ((j - i) % 3 === 1) { + return false; + } + if ((j - i) % 3 === 2 && ++cnt2 > 1) { + return false; + } + i = j; + } + return cnt2 === 1; +}