Skip to content

Commit 2021052

Browse files
committed
1 parent 9809fcd commit 2021052

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1185,3 +1185,4 @@ mod s1567_maximum_length_of_subarray_with_positive_product;
11851185
mod s1568_minimum_number_of_days_to_disconnect_island;
11861186
mod s1569_number_of_ways_to_reorder_array_to_get_same_bst;
11871187
mod s1572_matrix_diagonal_sum;
1188+
mod s1573_number_of_ways_to_split_a_string;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* [1573] Number of Ways to Split a String
3+
*
4+
* Given a binary string s, you can split s into 3 non-empty strings s1, s2, and s3 where s1 + s2 + s3 = s.
5+
* Return the number of ways s can be split such that the number of ones is the same in s1, s2, and s3. Since the answer may be too large, return it modulo 10^9 + 7.
6+
*
7+
* Example 1:
8+
*
9+
* Input: s = "10101"
10+
* Output: 4
11+
* Explanation: There are four ways to split s in 3 parts where each part contain the same number of letters '1'.
12+
* "1|010|1"
13+
* "1|01|01"
14+
* "10|10|1"
15+
* "10|1|01"
16+
*
17+
* Example 2:
18+
*
19+
* Input: s = "1001"
20+
* Output: 0
21+
*
22+
* Example 3:
23+
*
24+
* Input: s = "0000"
25+
* Output: 3
26+
* Explanation: There are three ways to split s in 3 parts.
27+
* "0|0|00"
28+
* "0|00|0"
29+
* "00|0|0"
30+
*
31+
*
32+
* Constraints:
33+
*
34+
* 3 <= s.length <= 10^5
35+
* s[i] is either '0' or '1'.
36+
*
37+
*/
38+
pub struct Solution {}
39+
40+
// problem: https://leetcode.com/problems/number-of-ways-to-split-a-string/
41+
// discuss: https://leetcode.com/problems/number-of-ways-to-split-a-string/discuss/?currentPage=1&orderBy=most_votes&query=
42+
43+
// submission codes start here
44+
45+
impl Solution {
46+
// Credit: https://leetcode.com/problems/number-of-ways-to-split-a-string/solutions/3174473/just-a-runnable-solution/
47+
48+
const MOD: i32 = 1_000_000_007;
49+
50+
pub fn num_ways(s: String) -> i32 {
51+
let s = s.as_bytes();
52+
let n = s.len();
53+
let mut one = 0;
54+
let result;
55+
56+
for &item in s.iter().take(n) {
57+
if item == b'1' {
58+
one += 1;
59+
}
60+
}
61+
62+
if one % 3 != 0 {
63+
return 0;
64+
} else if one == 0 {
65+
result = ((n as i64 - 1) * (n as i64 - 2) / 2) % MOD;
66+
} else {
67+
let count_of_one = one / 3;
68+
let (mut index1, mut index1end, mut index2, mut index2end) = (-1, -1, -1, -1);
69+
let mut count = 0;
70+
for (i, &item) in s.iter().enumerate().take(n) {
71+
if item == b'1' {
72+
count += 1;
73+
}
74+
if count == count_of_one && index1 == -1 {
75+
index1 = i as i64;
76+
}
77+
if count == count_of_one + 1 && index2 == -1 {
78+
index2 = i as i64;
79+
}
80+
if count == count_of_one * 2 && index1end == -1 {
81+
index1end = i as i64;
82+
}
83+
if count == count_of_one * 2 + 1 && index2end == -1 {
84+
index2end = i as i64;
85+
}
86+
}
87+
result = ((index2 - index1) % MOD) * ((index2end - index1end) % MOD) % MOD;
88+
}
89+
90+
result as _
91+
}
92+
}
93+
94+
// submission codes end
95+
96+
#[cfg(test)]
97+
mod tests {
98+
use super::*;
99+
100+
#[test]
101+
fn test_1573_example_1() {
102+
let s = "10101".to_string();
103+
104+
let result = 4;
105+
106+
assert_eq!(Solution::num_ways(s), result);
107+
}
108+
109+
#[test]
110+
fn test_1573_example_2() {
111+
let s = "1001".to_string();
112+
113+
let result = 0;
114+
115+
assert_eq!(Solution::num_ways(s), result);
116+
}
117+
118+
#[test]
119+
fn test_1573_example_3() {
120+
let s = "0000".to_string();
121+
122+
let result = 3;
123+
124+
assert_eq!(Solution::num_ways(s), result);
125+
}
126+
}

0 commit comments

Comments
 (0)