Skip to content

Commit 396b2bf

Browse files
authored
Create minimum-changes-to-make-alternating-binary-string.cpp
1 parent ca19b0e commit 396b2bf

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int minOperations(string s) {
7+
int cnt = 0;
8+
for (int i = 0; i < size(s); ++i) {
9+
if (s[i] - '0' == i % 2) {
10+
++cnt;
11+
}
12+
}
13+
return min(cnt, int(size(s)) - cnt);
14+
}
15+
};

0 commit comments

Comments
 (0)