-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_reboot.cpp
More file actions
37 lines (31 loc) · 923 Bytes
/
Copy pathstring_reboot.cpp
File metadata and controls
37 lines (31 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <algorithm> // For min()
using namespace std;
class Solution {
public:
int minFlips(string s) {
int n = s.length();
int flipsForPattern0 = 0;
for (int i = 0; i < n; i++) {
// Pattern A: 0, 1, 0, 1...
// At even index (0, 2, 4), we expect '0'
// At odd index (1, 3, 5), we expect '1'
if (i % 2 == 0) { // Even index
if (s[i] != '0') flipsForPattern0++;
} else { // Odd index
if (s[i] != '1') flipsForPattern0++;
}
}
// flipsForPattern1 is just the total length minus the other pattern's flips
int flipsForPattern1 = n - flipsForPattern0;
return min(flipsForPattern0, flipsForPattern1);
}
};
int main() {
string s;
cin >> s;
Solution sol;
cout << sol.minFlips(s) << endl;
return 0;
}