Skip to content

Commit 58621b2

Browse files
committed
feat: add solutions to lc problem: No.1933
No.1933.Check if String Is Decomposable Into Value-Equal Substrings
1 parent ec5f156 commit 58621b2

File tree

9 files changed

+242
-8
lines changed

9 files changed

+242
-8
lines changed

solution/0800-0899/0870.Advantage Shuffle/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@
5656
class Solution:
5757
def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
5858
nums1.sort()
59-
t = [(v, i) for i, v in enumerate(nums2)]
60-
t.sort()
59+
t = sorted((v, i) for i, v in enumerate(nums2))
6160
n = len(nums2)
6261
ans = [0] * n
6362
i, j = 0, n - 1

solution/0800-0899/0870.Advantage Shuffle/README_EN.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
class Solution:
3636
def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
3737
nums1.sort()
38-
t = [(v, i) for i, v in enumerate(nums2)]
39-
t.sort()
38+
t = sorted((v, i) for i, v in enumerate(nums2))
4039
n = len(nums2)
4140
ans = [0] * n
4241
i, j = 0, n - 1

solution/0800-0899/0870.Advantage Shuffle/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
33
nums1.sort()
4-
t = [(v, i) for i, v in enumerate(nums2)]
5-
t.sort()
4+
t = sorted((v, i) for i, v in enumerate(nums2))
65
n = len(nums2)
76
ans = [0] * n
87
i, j = 0, n - 1

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

+88-1
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,109 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:双指针**
59+
60+
遍历字符串 $s$,用双指针 $i$ 和 $j$ 统计每个等值子字符串的长度。若长度模 $3$ 余 $1$,说明该子字符串长度不符合要求,返回 `false`;若长度模 $3$ 余 $2$,说明出现了长度为 $2$ 的子字符串,若此前已经出现过长度为 $2$ 的子字符串,返回 `false`,否则将 $j$ 的值赋给 $i$,继续遍历。
61+
62+
遍历结束后,判断是否出现过长度为 $2$ 的子字符串,若没有,返回 `false`,否则返回 `true`
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
65+
5866
<!-- tabs:start -->
5967

6068
### **Python3**
6169

6270
<!-- 这里可写当前语言的特殊实现逻辑 -->
6371

6472
```python
65-
73+
class Solution:
74+
def isDecomposable(self, s: str) -> bool:
75+
i, n = 0, len(s)
76+
cnt2 = 0
77+
while i < n:
78+
j = i
79+
while j < n and s[j] == s[i]:
80+
j += 1
81+
if (j - i) % 3 == 1:
82+
return False
83+
cnt2 += (j - i) % 3 == 2
84+
if cnt2 > 1:
85+
return False
86+
i = j
87+
return cnt2 == 1
6688
```
6789

6890
### **Java**
6991

7092
<!-- 这里可写当前语言的特殊实现逻辑 -->
7193

7294
```java
95+
class Solution {
96+
public boolean isDecomposable(String s) {
97+
int i = 0, n = s.length();
98+
int cnt2 = 0;
99+
while (i < n) {
100+
int j = i;
101+
while (j < n && s.charAt(j) == s.charAt(i)) {
102+
++j;
103+
}
104+
if ((j - i) % 3 == 1) {
105+
return false;
106+
}
107+
if ((j - i) % 3 == 2 && ++cnt2 > 1) {
108+
return false;
109+
}
110+
i = j;
111+
}
112+
return cnt2 == 1;
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
bool isDecomposable(string s) {
123+
int i = 0, n = s.size();
124+
int cnt2 = 0;
125+
while (i < n) {
126+
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;
130+
i = j;
131+
}
132+
return cnt2 == 1;
133+
}
134+
};
135+
```
73136
137+
### **Go**
138+
139+
```go
140+
func isDecomposable(s string) bool {
141+
i, n := 0, len(s)
142+
cnt2 := 0
143+
for i < n {
144+
j := i
145+
for j < n && s[j] == s[i] {
146+
j++
147+
}
148+
if (j-i)%3 == 1 {
149+
return false
150+
}
151+
if (j-i)%3 == 2 {
152+
cnt2++
153+
if cnt2 > 1 {
154+
return false
155+
}
156+
}
157+
i = j
158+
}
159+
return cnt2 == 1
160+
}
74161
```
75162

76163
### **...**

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

+80-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,92 @@
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def isDecomposable(self, s: str) -> bool:
62+
i, n = 0, len(s)
63+
cnt2 = 0
64+
while i < n:
65+
j = i
66+
while j < n and s[j] == s[i]:
67+
j += 1
68+
if (j - i) % 3 == 1:
69+
return False
70+
cnt2 += (j - i) % 3 == 2
71+
if cnt2 > 1:
72+
return False
73+
i = j
74+
return cnt2 == 1
6175
```
6276

6377
### **Java**
6478

6579
```java
80+
class Solution {
81+
public boolean isDecomposable(String s) {
82+
int i = 0, n = s.length();
83+
int cnt2 = 0;
84+
while (i < n) {
85+
int j = i;
86+
while (j < n && s.charAt(j) == s.charAt(i)) {
87+
++j;
88+
}
89+
if ((j - i) % 3 == 1) {
90+
return false;
91+
}
92+
if ((j - i) % 3 == 2 && ++cnt2 > 1) {
93+
return false;
94+
}
95+
i = j;
96+
}
97+
return cnt2 == 1;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
bool isDecomposable(string s) {
108+
int i = 0, n = s.size();
109+
int cnt2 = 0;
110+
while (i < n) {
111+
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;
115+
i = j;
116+
}
117+
return cnt2 == 1;
118+
}
119+
};
120+
```
66121
122+
### **Go**
123+
124+
```go
125+
func isDecomposable(s string) bool {
126+
i, n := 0, len(s)
127+
cnt2 := 0
128+
for i < n {
129+
j := i
130+
for j < n && s[j] == s[i] {
131+
j++
132+
}
133+
if (j-i)%3 == 1 {
134+
return false
135+
}
136+
if (j-i)%3 == 2 {
137+
cnt2++
138+
if cnt2 > 1 {
139+
return false
140+
}
141+
}
142+
i = j
143+
}
144+
return cnt2 == 1
145+
}
67146
```
68147

69148
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func isDecomposable(s string) bool {
2+
i, n := 0, len(s)
3+
cnt2 := 0
4+
for i < n {
5+
j := i
6+
for 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 {
13+
cnt2++
14+
if cnt2 > 1 {
15+
return false
16+
}
17+
}
18+
i = j
19+
}
20+
return cnt2 == 1
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean isDecomposable(String s) {
3+
int i = 0, n = s.length();
4+
int cnt2 = 0;
5+
while (i < n) {
6+
int j = i;
7+
while (j < n && s.charAt(j) == s.charAt(i)) {
8+
++j;
9+
}
10+
if ((j - i) % 3 == 1) {
11+
return false;
12+
}
13+
if ((j - i) % 3 == 2 && ++cnt2 > 1) {
14+
return false;
15+
}
16+
i = j;
17+
}
18+
return cnt2 == 1;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def isDecomposable(self, s: str) -> bool:
3+
i, n = 0, len(s)
4+
cnt2 = 0
5+
while i < n:
6+
j = i
7+
while j < n and s[j] == s[i]:
8+
j += 1
9+
if (j - i) % 3 == 1:
10+
return False
11+
cnt2 += (j - i) % 3 == 2
12+
if cnt2 > 1:
13+
return False
14+
i = j
15+
return cnt2 == 1

0 commit comments

Comments
 (0)