mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
java: add “1758. Minimum Changes To Make Alternating Binary String”
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
835b0208b9
commit
f0e0adde9f
1 changed files with 21 additions and 0 deletions
21
java/minimum-changes-to-make-alternating-binary-string.java
Normal file
21
java/minimum-changes-to-make-alternating-binary-string.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
class Solution {
|
||||
public int minOperations(String s) {
|
||||
int startingZero = 0;
|
||||
int startingOne = 0;
|
||||
|
||||
for (int i = 0; i < s.length(); ++i) {
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c != '0' + i % 2) {
|
||||
++startingZero;
|
||||
}
|
||||
|
||||
if (c != '0' + (i + 1) % 2) {
|
||||
++startingOne;
|
||||
}
|
||||
}
|
||||
|
||||
return Math.min(startingZero, startingOne);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in a new issue