File tree 5 files changed +120
-0
lines changed
solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String
5 files changed +120
-0
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,51 @@ func min(a, b int) int {
119
119
}
120
120
```
121
121
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
+
122
167
### **...**
123
168
124
169
```
Original file line number Diff line number Diff line change @@ -104,6 +104,51 @@ func min(a, b int) int {
104
104
}
105
105
```
106
106
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
+
107
152
### **...**
108
153
109
154
```
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments