Skip to content

Commit 5db27a4

Browse files
authored
feat: add typescript solution to lc problem: No.1888.Minimum Number of Flips to Make the Binary String Alternating (doocs#441)
1 parent 2f92d64 commit 5db27a4

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

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

+20
Original file line numberDiff line numberDiff 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
```

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

+20
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
};

0 commit comments

Comments
 (0)