File tree 3 files changed +55
-0
lines changed
solution/1800-1899/1888.Minimum Number of Flips to Make the Binary String Alternating
3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,26 @@ class Solution {
108
108
}
109
109
```
110
110
111
+ ### ** TypeScript**
112
+
113
+ ``` ts
114
+ function minFlips(s : string ): number {
115
+ const n: number = s .length ;
116
+ const target: string [] = [' 0' , ' 1' ];
117
+ let count: number = 0 ;
118
+ for (let i: number = 0 ; i < n ; ++ i ) {
119
+ count += (s .charAt (i ) == target [i & 1 ] ? 0 : 1 );
120
+ }
121
+ let res = Math .min (count , n - count );
122
+ for (let i: number = 0 ; i < n ; ++ i ) {
123
+ count -= (s .charAt (i ) == target [i & 1 ] ? 0 : 1 );
124
+ count += (s .charAt (i ) == target [(i + n ) & 1 ] ? 0 : 1 );
125
+ res = Math .min (res , count , n - count );
126
+ }
127
+ return res ;
128
+ };
129
+ ```
130
+
111
131
### ** ...**
112
132
113
133
```
Original file line number Diff line number Diff line change @@ -100,6 +100,26 @@ class Solution {
100
100
}
101
101
```
102
102
103
+ ### ** TypeScript**
104
+
105
+ ``` ts
106
+ function minFlips(s : string ): number {
107
+ const n: number = s .length ;
108
+ const target: string [] = [' 0' , ' 1' ];
109
+ let count: number = 0 ;
110
+ for (let i: number = 0 ; i < n ; ++ i ) {
111
+ count += (s .charAt (i ) == target [i & 1 ] ? 0 : 1 );
112
+ }
113
+ let res = Math .min (count , n - count );
114
+ for (let i: number = 0 ; i < n ; ++ i ) {
115
+ count -= (s .charAt (i ) == target [i & 1 ] ? 0 : 1 );
116
+ count += (s .charAt (i ) == target [(i + n ) & 1 ] ? 0 : 1 );
117
+ res = Math .min (res , count , n - count );
118
+ }
119
+ return res ;
120
+ };
121
+ ```
122
+
103
123
### ** ...**
104
124
105
125
```
Original file line number Diff line number Diff line change
1
+ function minFlips ( s : string ) : number {
2
+ const n : number = s . length ;
3
+ const target : string [ ] = [ '0' , '1' ] ;
4
+ let count : number = 0 ;
5
+ for ( let i : number = 0 ; i < n ; ++ i ) {
6
+ count += ( s . charAt ( i ) == target [ i & 1 ] ? 0 : 1 ) ;
7
+ }
8
+ let res = Math . min ( count , n - count ) ;
9
+ for ( let i : number = 0 ; i < n ; ++ i ) {
10
+ count -= ( s . charAt ( i ) == target [ i & 1 ] ? 0 : 1 ) ;
11
+ count += ( s . charAt ( i ) == target [ ( i + n ) & 1 ] ? 0 : 1 ) ;
12
+ res = Math . min ( res , count , n - count ) ;
13
+ }
14
+ return res ;
15
+ } ;
You can’t perform that action at this time.
0 commit comments