Skip to content

Commit d14c811

Browse files
committed
feat: add solutions to lc problem: No.1573
No.1573.Number of Ways to Split a String
1 parent 23d3dc3 commit d14c811

File tree

7 files changed

+379
-16
lines changed

7 files changed

+379
-16
lines changed

solution/0900-0999/0927.Three Equal Parts/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@
8383

8484
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 表示 `arr` 的长度。
8585

86+
相似题目:
87+
88+
- [1573. 分割字符串的方案数](/solution/1500-1599/1573.Number%20of%20Ways%20to%20Split%20a%20String/README.md)
89+
8690
<!-- tabs:start -->
8791

8892
### **Python3**

solution/1500-1599/1573.Number of Ways to Split a String/README.md

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,163 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:计数**
64+
65+
我们先遍历字符串 $s$,统计其中字符 $1$ 的个数 $cnt$,如果 $cnt$ 不能被 $3$ 整除,那么无法分割,直接返回 $0$。如果 $cnt$ 为 $0$,说明字符串中没有字符 $1$,我们可以在 $n-1$ 个位置中任意选择两个位置,将字符串分割成三个子串,那么方案数就是 $C_{n-1}^2$。
66+
67+
如果 $cnt \gt 0$,我们将 $cnt$ 更新为 $\frac{cnt}{3}$,即每个子串中字符 $1$ 的个数。
68+
69+
接下来我们找到第一个子字符串的右边界的最小下标,记为 $i_1$,第一个子字符串的右边界的最大下标(不包含),记为 $i_2$;第二个子字符串的右边界的最小下标,记为 $j_1$,第二个子字符串的右边界的最大下标(不包含),记为 $j_2$。那么方案数就是 $(i_2-i_1) \times (j_2-j_1)$。
70+
71+
注意答案可能很大,需要对 $10^9+7$ 取模。
72+
73+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
74+
75+
相似题目:
76+
77+
- [927. 三等分](/solution/0900-0999/0927.Three%20Equal%20Parts/README.md)
78+
6379
<!-- tabs:start -->
6480

6581
### **Python3**
6682

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

6985
```python
70-
86+
class Solution:
87+
def numWays(self, s: str) -> int:
88+
def find(x):
89+
t = 0
90+
for i, c in enumerate(s):
91+
t += int(c == '1')
92+
if t == x:
93+
return i
94+
cnt, m = divmod(sum(c == '1' for c in s), 3)
95+
if m:
96+
return 0
97+
n = len(s)
98+
mod = 10**9 + 7
99+
if cnt == 0:
100+
return ((n - 1) * (n - 2) // 2) % mod
101+
i1, i2 = find(cnt), find(cnt + 1)
102+
j1, j2 = find(cnt * 2), find(cnt * 2 + 1)
103+
return (i2 - i1) * (j2 - j1) % (10**9 + 7)
71104
```
72105

73106
### **Java**
74107

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

77110
```java
111+
class Solution {
112+
private String s;
113+
114+
public int numWays(String s) {
115+
this.s = s;
116+
int cnt = 0;
117+
int n = s.length();
118+
for (int i = 0; i < n; ++i) {
119+
if (s.charAt(i) == '1') {
120+
++cnt;
121+
}
122+
}
123+
int m = cnt % 3;
124+
if (m != 0) {
125+
return 0;
126+
}
127+
final int mod = (int) 1e9 + 7;
128+
if (cnt == 0) {
129+
return (int) (((n - 1L) * (n - 2) / 2) % mod);
130+
}
131+
cnt /= 3;
132+
long i1 = find(cnt), i2 = find(cnt + 1);
133+
long j1 = find(cnt * 2), j2 = find(cnt * 2 + 1);
134+
return (int) ((i2 - i1) * (j2 - j1) % mod);
135+
}
136+
137+
private int find(int x) {
138+
int t = 0;
139+
for (int i = 0;; ++i) {
140+
t += s.charAt(i) == '1' ? 1 : 0;
141+
if (t == x) {
142+
return i;
143+
}
144+
}
145+
}
146+
}
147+
```
148+
149+
### **C++**
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
int numWays(string s) {
155+
int cnt = 0;
156+
for (char& c : s) {
157+
cnt += c == '1';
158+
}
159+
int m = cnt % 3;
160+
if (m) {
161+
return 0;
162+
}
163+
const int mod = 1e9 + 7;
164+
int n = s.size();
165+
if (cnt == 0) {
166+
return (n - 1LL) * (n - 2) / 2 % mod;
167+
}
168+
cnt /= 3;
169+
auto find = [&](int x) {
170+
int t = 0;
171+
for (int i = 0; ; ++i) {
172+
t += s[i] == '1';
173+
if (t == x) {
174+
return i;
175+
}
176+
}
177+
};
178+
int i1 = find(cnt), i2 = find(cnt + 1);
179+
int j1 = find(cnt * 2), j2 = find(cnt * 2 + 1);
180+
return (1LL * (i2 - i1) * (j2 - j1)) % mod;
181+
}
182+
};
183+
```
78184
185+
### **Go**
186+
187+
```go
188+
func numWays(s string) int {
189+
cnt := 0
190+
for _, c := range s {
191+
if c == '1' {
192+
cnt++
193+
}
194+
}
195+
m := cnt % 3
196+
if m != 0 {
197+
return 0
198+
}
199+
const mod = 1e9 + 7
200+
n := len(s)
201+
if cnt == 0 {
202+
return (n - 1) * (n - 2) / 2 % mod
203+
}
204+
cnt /= 3
205+
find := func(x int) int {
206+
t := 0
207+
for i := 0; ; i++ {
208+
if s[i] == '1' {
209+
t++
210+
if t == x {
211+
return i
212+
}
213+
}
214+
}
215+
}
216+
i1, i2 := find(cnt), find(cnt+1)
217+
j1, j2 := find(cnt*2), find(cnt*2+1)
218+
return (i2 - i1) * (j2 - j1) % mod
219+
}
79220
```
80221

81222
### **...**

solution/1500-1599/1573.Number of Ways to Split a String/README_EN.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,138 @@
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def numWays(self, s: str) -> int:
59+
def find(x):
60+
t = 0
61+
for i, c in enumerate(s):
62+
t += int(c == '1')
63+
if t == x:
64+
return i
65+
cnt, m = divmod(sum(c == '1' for c in s), 3)
66+
if m:
67+
return 0
68+
n = len(s)
69+
mod = 10**9 + 7
70+
if cnt == 0:
71+
return ((n - 1) * (n - 2) // 2) % mod
72+
i1, i2 = find(cnt), find(cnt + 1)
73+
j1, j2 = find(cnt * 2), find(cnt * 2 + 1)
74+
return (i2 - i1) * (j2 - j1) % (10**9 + 7)
5875
```
5976

6077
### **Java**
6178

6279
```java
80+
class Solution {
81+
private String s;
82+
83+
public int numWays(String s) {
84+
this.s = s;
85+
int cnt = 0;
86+
int n = s.length();
87+
for (int i = 0; i < n; ++i) {
88+
if (s.charAt(i) == '1') {
89+
++cnt;
90+
}
91+
}
92+
int m = cnt % 3;
93+
if (m != 0) {
94+
return 0;
95+
}
96+
final int mod = (int) 1e9 + 7;
97+
if (cnt == 0) {
98+
return (int) (((n - 1L) * (n - 2) / 2) % mod);
99+
}
100+
cnt /= 3;
101+
long i1 = find(cnt), i2 = find(cnt + 1);
102+
long j1 = find(cnt * 2), j2 = find(cnt * 2 + 1);
103+
return (int) ((i2 - i1) * (j2 - j1) % mod);
104+
}
105+
106+
private int find(int x) {
107+
int t = 0;
108+
for (int i = 0;; ++i) {
109+
t += s.charAt(i) == '1' ? 1 : 0;
110+
if (t == x) {
111+
return i;
112+
}
113+
}
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
int numWays(string s) {
124+
int cnt = 0;
125+
for (char& c : s) {
126+
cnt += c == '1';
127+
}
128+
int m = cnt % 3;
129+
if (m) {
130+
return 0;
131+
}
132+
const int mod = 1e9 + 7;
133+
int n = s.size();
134+
if (cnt == 0) {
135+
return (n - 1LL) * (n - 2) / 2 % mod;
136+
}
137+
cnt /= 3;
138+
auto find = [&](int x) {
139+
int t = 0;
140+
for (int i = 0; ; ++i) {
141+
t += s[i] == '1';
142+
if (t == x) {
143+
return i;
144+
}
145+
}
146+
};
147+
int i1 = find(cnt), i2 = find(cnt + 1);
148+
int j1 = find(cnt * 2), j2 = find(cnt * 2 + 1);
149+
return (1LL * (i2 - i1) * (j2 - j1)) % mod;
150+
}
151+
};
152+
```
63153
154+
### **Go**
155+
156+
```go
157+
func numWays(s string) int {
158+
cnt := 0
159+
for _, c := range s {
160+
if c == '1' {
161+
cnt++
162+
}
163+
}
164+
m := cnt % 3
165+
if m != 0 {
166+
return 0
167+
}
168+
const mod = 1e9 + 7
169+
n := len(s)
170+
if cnt == 0 {
171+
return (n - 1) * (n - 2) / 2 % mod
172+
}
173+
cnt /= 3
174+
find := func(x int) int {
175+
t := 0
176+
for i := 0; ; i++ {
177+
if s[i] == '1' {
178+
t++
179+
if t == x {
180+
return i
181+
}
182+
}
183+
}
184+
}
185+
i1, i2 := find(cnt), find(cnt+1)
186+
j1, j2 := find(cnt*2), find(cnt*2+1)
187+
return (i2 - i1) * (j2 - j1) % mod
188+
}
64189
```
65190

66191
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int numWays(string s) {
4+
int cnt = 0;
5+
for (char& c : s) {
6+
cnt += c == '1';
7+
}
8+
int m = cnt % 3;
9+
if (m) {
10+
return 0;
11+
}
12+
const int mod = 1e9 + 7;
13+
int n = s.size();
14+
if (cnt == 0) {
15+
return (n - 1LL) * (n - 2) / 2 % mod;
16+
}
17+
cnt /= 3;
18+
auto find = [&](int x) {
19+
int t = 0;
20+
for (int i = 0; ; ++i) {
21+
t += s[i] == '1';
22+
if (t == x) {
23+
return i;
24+
}
25+
}
26+
};
27+
int i1 = find(cnt), i2 = find(cnt + 1);
28+
int j1 = find(cnt * 2), j2 = find(cnt * 2 + 1);
29+
return (1LL * (i2 - i1) * (j2 - j1)) % mod;
30+
}
31+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func numWays(s string) int {
2+
cnt := 0
3+
for _, c := range s {
4+
if c == '1' {
5+
cnt++
6+
}
7+
}
8+
m := cnt % 3
9+
if m != 0 {
10+
return 0
11+
}
12+
const mod = 1e9 + 7
13+
n := len(s)
14+
if cnt == 0 {
15+
return (n - 1) * (n - 2) / 2 % mod
16+
}
17+
cnt /= 3
18+
find := func(x int) int {
19+
t := 0
20+
for i := 0; ; i++ {
21+
if s[i] == '1' {
22+
t++
23+
if t == x {
24+
return i
25+
}
26+
}
27+
}
28+
}
29+
i1, i2 := find(cnt), find(cnt+1)
30+
j1, j2 := find(cnt*2), find(cnt*2+1)
31+
return (i2 - i1) * (j2 - j1) % mod
32+
}

0 commit comments

Comments
 (0)