Skip to content

Commit 0e1c5e6

Browse files
authored
feat: add solutions for lc No.1888 (#5065)
1 parent 0226659 commit 0e1c5e6

5 files changed

Lines changed: 289 additions & 1 deletion

File tree

solution/1800-1899/1888.Minimum Number of Flips to Make the Binary String Alternating/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ tags:
8080

8181
因此,我们只需要枚举每个长度为 $n$ 的子串,计算将其变成交替二进制串的代价,取最小值即可。
8282

83+
我们可以预先计算出字符串 $s$ 与两种交替二进制串的差异数量,记为 $\textit{cnt}$,则将 $s$ 变成第一种交替二进制串的代价为 $\textit{cnt}$,将 $s$ 变成第二种交替二进制串的代价为 $n - \textit{cnt}$。我们初始化 $\textit{ans} = \min(\textit{cnt}, n - \textit{cnt})$。
84+
85+
接下来,我们枚举每个长度为 $n$ 的子串,更新 $\textit{cnt}$ 的值。对于每个位置 $i$,我们将 $s[i]$ 从第一种交替二进制串的差异数量中减去,并将 $s[i]$ 加入到第二种交替二进制串的差异数量中。更新 $\textit{ans} = \min(\textit{ans}, \textit{cnt}, n - \textit{cnt})$。
86+
87+
最后返回 $\textit{ans}$ 即可。
88+
8389
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
8490

8591
<!-- tabs:start -->
@@ -209,6 +215,101 @@ function minFlips(s: string): number {
209215
}
210216
```
211217

218+
#### Rust
219+
220+
```rust
221+
impl Solution {
222+
pub fn min_flips(s: String) -> i32 {
223+
let n: usize = s.len();
224+
let bytes = s.as_bytes();
225+
let target = b"01";
226+
let mut cnt: i32 = 0;
227+
228+
for i in 0..n {
229+
if bytes[i] != target[i & 1] {
230+
cnt += 1;
231+
}
232+
}
233+
234+
let mut ans = cnt.min(n as i32 - cnt);
235+
236+
for i in 0..n {
237+
if bytes[i] != target[i & 1] {
238+
cnt -= 1;
239+
}
240+
if bytes[i] != target[(i + n) & 1] {
241+
cnt += 1;
242+
}
243+
ans = ans.min(cnt).min(n as i32 - cnt);
244+
}
245+
246+
ans
247+
}
248+
}
249+
```
250+
251+
#### JavaScript
252+
253+
```js
254+
/**
255+
* @param {string} s
256+
* @return {number}
257+
*/
258+
var minFlips = function (s) {
259+
const n = s.length;
260+
const target = '01';
261+
let cnt = 0;
262+
for (let i = 0; i < n; ++i) {
263+
if (s[i] !== target[i & 1]) {
264+
++cnt;
265+
}
266+
}
267+
let ans = Math.min(cnt, n - cnt);
268+
for (let i = 0; i < n; ++i) {
269+
if (s[i] !== target[i & 1]) {
270+
--cnt;
271+
}
272+
if (s[i] !== target[(i + n) & 1]) {
273+
++cnt;
274+
}
275+
ans = Math.min(ans, cnt, n - cnt);
276+
}
277+
return ans;
278+
};
279+
```
280+
281+
#### C#
282+
283+
```cs
284+
public class Solution {
285+
public int MinFlips(string s) {
286+
int n = s.Length;
287+
string target = "01";
288+
int cnt = 0;
289+
290+
for (int i = 0; i < n; ++i) {
291+
if (s[i] != target[i & 1]) {
292+
++cnt;
293+
}
294+
}
295+
296+
int ans = Math.Min(cnt, n - cnt);
297+
298+
for (int i = 0; i < n; ++i) {
299+
if (s[i] != target[i & 1]) {
300+
--cnt;
301+
}
302+
if (s[i] != target[(i + n) & 1]) {
303+
++cnt;
304+
}
305+
ans = Math.Min(ans, Math.Min(cnt, n - cnt));
306+
}
307+
308+
return ans;
309+
}
310+
}
311+
```
312+
212313
<!-- tabs:end -->
213314

214315
<!-- solution:end -->

solution/1800-1899/1888.Minimum Number of Flips to Make the Binary String Alternating/README_EN.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,19 @@ Then, use the second operation on the third and sixth elements to make s = &quot
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Sliding Window
79+
80+
We notice that operation $1$ effectively turns the string into a cycle, and operation $2$ makes a substring of length $n$ within the cycle into an alternating binary string.
81+
82+
Therefore, we only need to enumerate each substring of length $n$, calculate the cost to make it an alternating binary string, and take the minimum.
83+
84+
We can pre-calculate the number of differences between string $s$ and the two types of alternating binary strings, denoted as $\textit{cnt}$. The cost to make $s$ into the first type of alternating binary string is $\textit{cnt}$, and the cost to make $s$ into the second type is $n - \textit{cnt}$. We initialize $\textit{ans} = \min(\textit{cnt}, n - \textit{cnt})$.
85+
86+
Next, we enumerate each substring of length $n$ and update the value of $\textit{cnt}$. For each position $i$, we subtract the difference of $s[i]$ from the first type of alternating binary string, and add the difference of $s[i]$ to the second type. We update $\textit{ans} = \min(\textit{ans}, \textit{cnt}, n - \textit{cnt})$.
87+
88+
Finally, return $\textit{ans}$.
89+
90+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
7991

8092
<!-- tabs:start -->
8193

@@ -204,6 +216,101 @@ function minFlips(s: string): number {
204216
}
205217
```
206218

219+
#### Rust
220+
221+
```rust
222+
impl Solution {
223+
pub fn min_flips(s: String) -> i32 {
224+
let n: usize = s.len();
225+
let bytes = s.as_bytes();
226+
let target = b"01";
227+
let mut cnt: i32 = 0;
228+
229+
for i in 0..n {
230+
if bytes[i] != target[i & 1] {
231+
cnt += 1;
232+
}
233+
}
234+
235+
let mut ans = cnt.min(n as i32 - cnt);
236+
237+
for i in 0..n {
238+
if bytes[i] != target[i & 1] {
239+
cnt -= 1;
240+
}
241+
if bytes[i] != target[(i + n) & 1] {
242+
cnt += 1;
243+
}
244+
ans = ans.min(cnt).min(n as i32 - cnt);
245+
}
246+
247+
ans
248+
}
249+
}
250+
```
251+
252+
#### JavaScript
253+
254+
```js
255+
/**
256+
* @param {string} s
257+
* @return {number}
258+
*/
259+
var minFlips = function (s) {
260+
const n = s.length;
261+
const target = '01';
262+
let cnt = 0;
263+
for (let i = 0; i < n; ++i) {
264+
if (s[i] !== target[i & 1]) {
265+
++cnt;
266+
}
267+
}
268+
let ans = Math.min(cnt, n - cnt);
269+
for (let i = 0; i < n; ++i) {
270+
if (s[i] !== target[i & 1]) {
271+
--cnt;
272+
}
273+
if (s[i] !== target[(i + n) & 1]) {
274+
++cnt;
275+
}
276+
ans = Math.min(ans, cnt, n - cnt);
277+
}
278+
return ans;
279+
};
280+
```
281+
282+
#### C#
283+
284+
```cs
285+
public class Solution {
286+
public int MinFlips(string s) {
287+
int n = s.Length;
288+
string target = "01";
289+
int cnt = 0;
290+
291+
for (int i = 0; i < n; ++i) {
292+
if (s[i] != target[i & 1]) {
293+
++cnt;
294+
}
295+
}
296+
297+
int ans = Math.Min(cnt, n - cnt);
298+
299+
for (int i = 0; i < n; ++i) {
300+
if (s[i] != target[i & 1]) {
301+
--cnt;
302+
}
303+
if (s[i] != target[(i + n) & 1]) {
304+
++cnt;
305+
}
306+
ans = Math.Min(ans, Math.Min(cnt, n - cnt));
307+
}
308+
309+
return ans;
310+
}
311+
}
312+
```
313+
207314
<!-- tabs:end -->
208315

209316
<!-- solution:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
public int MinFlips(string s) {
3+
int n = s.Length;
4+
string target = "01";
5+
int cnt = 0;
6+
7+
for (int i = 0; i < n; ++i) {
8+
if (s[i] != target[i & 1]) {
9+
++cnt;
10+
}
11+
}
12+
13+
int ans = Math.Min(cnt, n - cnt);
14+
15+
for (int i = 0; i < n; ++i) {
16+
if (s[i] != target[i & 1]) {
17+
--cnt;
18+
}
19+
if (s[i] != target[(i + n) & 1]) {
20+
++cnt;
21+
}
22+
ans = Math.Min(ans, Math.Min(cnt, n - cnt));
23+
}
24+
25+
return ans;
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var minFlips = function (s) {
6+
const n = s.length;
7+
const target = '01';
8+
let cnt = 0;
9+
for (let i = 0; i < n; ++i) {
10+
if (s[i] !== target[i & 1]) {
11+
++cnt;
12+
}
13+
}
14+
let ans = Math.min(cnt, n - cnt);
15+
for (let i = 0; i < n; ++i) {
16+
if (s[i] !== target[i & 1]) {
17+
--cnt;
18+
}
19+
if (s[i] !== target[(i + n) & 1]) {
20+
++cnt;
21+
}
22+
ans = Math.min(ans, cnt, n - cnt);
23+
}
24+
return ans;
25+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn min_flips(s: String) -> i32 {
3+
let n: usize = s.len();
4+
let bytes = s.as_bytes();
5+
let target = b"01";
6+
let mut cnt: i32 = 0;
7+
8+
for i in 0..n {
9+
if bytes[i] != target[i & 1] {
10+
cnt += 1;
11+
}
12+
}
13+
14+
let mut ans = cnt.min(n as i32 - cnt);
15+
16+
for i in 0..n {
17+
if bytes[i] != target[i & 1] {
18+
cnt -= 1;
19+
}
20+
if bytes[i] != target[(i + n) & 1] {
21+
cnt += 1;
22+
}
23+
ans = ans.min(cnt).min(n as i32 - cnt);
24+
}
25+
26+
ans
27+
}
28+
}

0 commit comments

Comments
 (0)