File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed
solution/1800-1899/1888.Minimum Number of Flips to Make the Binary String Alternating Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,26 @@ class Solution {
108108}
109109```
110110
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+
111131### ** ...**
112132
113133```
Original file line number Diff line number Diff line change @@ -100,6 +100,26 @@ class Solution {
100100}
101101```
102102
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+
103123### ** ...**
104124
105125```
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