Skip to content

Commit 69398f9

Browse files
committed
feat: add solutions to lc problem: No.1758
No.1758.Minimum Changes To Make Alternating Binary String
1 parent e0839e0 commit 69398f9

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,51 @@ func min(a, b int) int {
119119
}
120120
```
121121

122+
### **TypeScript**
123+
124+
```ts
125+
function minOperations(s: string): number {
126+
const n = s.length;
127+
let count = 0;
128+
for (let i = 0; i < n; i++) {
129+
count += s[i] !== '01'[i & 1] ? 1 : 0;
130+
}
131+
return Math.min(count, n - count);
132+
}
133+
```
134+
135+
### **Rust**
136+
137+
```rust
138+
impl Solution {
139+
pub fn min_operations(s: String) -> i32 {
140+
let n = s.len();
141+
let s = s.as_bytes();
142+
let cs = [b'0', b'1'];
143+
let mut count = 0;
144+
for i in 0..n {
145+
count += if s[i] != cs[i & 1] { 1 } else { 0 };
146+
}
147+
count.min(n - count) as i32
148+
}
149+
}
150+
```
151+
152+
### **C**
153+
154+
```c
155+
#define min(a, b) (((a) < (b)) ? (a) : (b))
156+
157+
int minOperations(char *s) {
158+
int n = strlen(s);
159+
int count = 0;
160+
for (int i = 0; i < n; i++) {
161+
count += s[i] != ('0' + (i & 1)) ? 0 : 1;
162+
}
163+
return min(count, n - count);
164+
}
165+
```
166+
122167
### **...**
123168
124169
```

solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String/README_EN.md

+45
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,51 @@ func min(a, b int) int {
104104
}
105105
```
106106

107+
### **TypeScript**
108+
109+
```ts
110+
function minOperations(s: string): number {
111+
const n = s.length;
112+
let count = 0;
113+
for (let i = 0; i < n; i++) {
114+
count += s[i] !== '01'[i & 1] ? 1 : 0;
115+
}
116+
return Math.min(count, n - count);
117+
}
118+
```
119+
120+
### **Rust**
121+
122+
```rust
123+
impl Solution {
124+
pub fn min_operations(s: String) -> i32 {
125+
let n = s.len();
126+
let s = s.as_bytes();
127+
let cs = [b'0', b'1'];
128+
let mut count = 0;
129+
for i in 0..n {
130+
count += if s[i] != cs[i & 1] { 1 } else { 0 };
131+
}
132+
count.min(n - count) as i32
133+
}
134+
}
135+
```
136+
137+
### **C**
138+
139+
```c
140+
#define min(a, b) (((a) < (b)) ? (a) : (b))
141+
142+
int minOperations(char *s) {
143+
int n = strlen(s);
144+
int count = 0;
145+
for (int i = 0; i < n; i++) {
146+
count += s[i] != ('0' + (i & 1)) ? 0 : 1;
147+
}
148+
return min(count, n - count);
149+
}
150+
```
151+
107152
### **...**
108153
109154
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#define min(a, b) (((a) < (b)) ? (a) : (b))
2+
3+
int minOperations(char *s) {
4+
int n = strlen(s);
5+
int count = 0;
6+
for (int i = 0; i < n; i++) {
7+
count += s[i] != ('0' + (i & 1)) ? 0 : 1;
8+
}
9+
return min(count, n - count);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn min_operations(s: String) -> i32 {
3+
let n = s.len();
4+
let s = s.as_bytes();
5+
let cs = [b'0', b'1'];
6+
let mut count = 0;
7+
for i in 0..n {
8+
count += if s[i] != cs[i & 1] { 1 } else { 0 };
9+
}
10+
count.min(n - count) as i32
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function minOperations(s: string): number {
2+
const n = s.length;
3+
let count = 0;
4+
for (let i = 0; i < n; i++) {
5+
count += s[i] !== '01'[i & 1] ? 1 : 0;
6+
}
7+
return Math.min(count, n - count);
8+
}

0 commit comments

Comments
 (0)