Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5610d9e

Browse files
feat: add solutions to lc problem: No.1933 (doocs#2000)
No.1933.Check if String Is Decomposable Into Value-Equal Substrings
1 parent 7061c5b commit 5610d9e

File tree

4 files changed

+143
-25
lines changed

4 files changed

+143
-25
lines changed

‎solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README.md‎

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
遍历结束后,判断是否出现过长度为 2ドル$ 的子字符串,若没有,返回 `false`,否则返回 `true`
6363

64-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
64+
时间复杂度 $O(n),ドル其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$
6565

6666
<!-- tabs:start -->
6767

@@ -87,6 +87,20 @@ class Solution:
8787
return cnt2 == 1
8888
```
8989

90+
```python
91+
class Solution:
92+
def isDecomposable(self, s: str) -> bool:
93+
cnt2 = 0
94+
for _, g in groupby(s):
95+
m = len(list(g))
96+
if m % 3 == 1:
97+
return False
98+
cnt2 += m % 3 == 2
99+
if cnt2 > 1:
100+
return False
101+
return cnt2 == 1
102+
```
103+
90104
### **Java**
91105

92106
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -120,13 +134,19 @@ class Solution {
120134
class Solution {
121135
public:
122136
bool isDecomposable(string s) {
123-
int i = 0, n = s.size();
124137
int cnt2 = 0;
125-
while (i < n) {
138+
for (int i = 0, n = s.size(); i < n;) {
126139
int j = i;
127-
while (j < n && s[j] == s[i]) ++j;
128-
if ((j - i) % 3 == 1) return false;
129-
if ((j - i) % 3 == 2 && ++cnt2 > 1) return false;
140+
while (j < n && s[j] == s[i]) {
141+
++j;
142+
}
143+
if ((j - i) % 3 == 1) {
144+
return false;
145+
}
146+
cnt2 += (j - i) % 3 == 2;
147+
if (cnt2 > 1) {
148+
return false;
149+
}
130150
i = j;
131151
}
132152
return cnt2 == 1;
@@ -160,6 +180,29 @@ func isDecomposable(s string) bool {
160180
}
161181
```
162182

183+
### **TypeScript**
184+
185+
```ts
186+
function isDecomposable(s: string): boolean {
187+
const n = s.length;
188+
let cnt2 = 0;
189+
for (let i = 0; i < n; ) {
190+
let j = i;
191+
while (j < n && s[j] === s[i]) {
192+
++j;
193+
}
194+
if ((j - i) % 3 === 1) {
195+
return false;
196+
}
197+
if ((j - i) % 3 === 2 && ++cnt2 > 1) {
198+
return false;
199+
}
200+
i = j;
201+
}
202+
return cnt2 === 1;
203+
}
204+
```
205+
163206
### **...**
164207

165208
```

‎solution/1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings/README_EN.md‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252

5353
## Solutions
5454

55+
**Solution 1: Two Pointers**
56+
57+
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.
58+
59+
After the traversal, check whether a substring of length 2ドル$ has appeared. If not, return `false`, otherwise return `true`.
60+
61+
The time complexity is $O(n),ドル where $n$ is the length of the string $s$. The space complexity is $O(1)$.
62+
5563
<!-- tabs:start -->
5664

5765
### **Python3**
@@ -74,6 +82,20 @@ class Solution:
7482
return cnt2 == 1
7583
```
7684

85+
```python
86+
class Solution:
87+
def isDecomposable(self, s: str) -> bool:
88+
cnt2 = 0
89+
for _, g in groupby(s):
90+
m = len(list(g))
91+
if m % 3 == 1:
92+
return False
93+
cnt2 += m % 3 == 2
94+
if cnt2 > 1:
95+
return False
96+
return cnt2 == 1
97+
```
98+
7799
### **Java**
78100

79101
```java
@@ -105,13 +127,19 @@ class Solution {
105127
class Solution {
106128
public:
107129
bool isDecomposable(string s) {
108-
int i = 0, n = s.size();
109130
int cnt2 = 0;
110-
while (i < n) {
131+
for (int i = 0, n = s.size(); i < n;) {
111132
int j = i;
112-
while (j < n && s[j] == s[i]) ++j;
113-
if ((j - i) % 3 == 1) return false;
114-
if ((j - i) % 3 == 2 && ++cnt2 > 1) return false;
133+
while (j < n && s[j] == s[i]) {
134+
++j;
135+
}
136+
if ((j - i) % 3 == 1) {
137+
return false;
138+
}
139+
cnt2 += (j - i) % 3 == 2;
140+
if (cnt2 > 1) {
141+
return false;
142+
}
115143
i = j;
116144
}
117145
return cnt2 == 1;
@@ -145,6 +173,29 @@ func isDecomposable(s string) bool {
145173
}
146174
```
147175

176+
### **TypeScript**
177+
178+
```ts
179+
function isDecomposable(s: string): boolean {
180+
const n = s.length;
181+
let cnt2 = 0;
182+
for (let i = 0; i < n; ) {
183+
let j = i;
184+
while (j < n && s[j] === s[i]) {
185+
++j;
186+
}
187+
if ((j - i) % 3 === 1) {
188+
return false;
189+
}
190+
if ((j - i) % 3 === 2 && ++cnt2 > 1) {
191+
return false;
192+
}
193+
i = j;
194+
}
195+
return cnt2 === 1;
196+
}
197+
```
198+
148199
### **...**
149200

150201
```
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
class Solution {
2-
public:
3-
bool isDecomposable(string s) {
4-
int i = 0, n = s.size();
5-
int cnt2 = 0;
6-
while (i < n) {
7-
int j = i;
8-
while (j < n && s[j] == s[i]) ++j;
9-
if ((j - i) % 3 == 1) return false;
10-
if ((j - i) % 3 == 2 && ++cnt2 > 1) return false;
11-
i = j;
12-
}
13-
return cnt2 == 1;
14-
}
1+
class Solution {
2+
public:
3+
bool isDecomposable(string s) {
4+
int cnt2 = 0;
5+
for (int i = 0, n = s.size(); i < n;) {
6+
int j = i;
7+
while (j < n && s[j] == s[i]) {
8+
++j;
9+
}
10+
if ((j - i) % 3 == 1) {
11+
return false;
12+
}
13+
cnt2 += (j - i) % 3 == 2;
14+
if (cnt2 > 1) {
15+
return false;
16+
}
17+
i = j;
18+
}
19+
return cnt2 == 1;
20+
}
1521
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function isDecomposable(s: string): boolean {
2+
const n = s.length;
3+
let cnt2 = 0;
4+
for (let i = 0; i < n; ) {
5+
let j = i;
6+
while (j < n && s[j] === s[i]) {
7+
++j;
8+
}
9+
if ((j - i) % 3 === 1) {
10+
return false;
11+
}
12+
if ((j - i) % 3 === 2 && ++cnt2 > 1) {
13+
return false;
14+
}
15+
i = j;
16+
}
17+
return cnt2 === 1;
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /