Input: s ="1001"Output: 2Explanation: We change s[1] to 1 and s[3] to 0 to get string "1100".It can be seen that the string "1100"is beautiful because we can partition it into "11|00".It can be proven that 2is the minimum number of changes needed to make the string beautiful.
Example 2:
1
2
3
4
5
Input: s ="10"Output: 1Explanation: We change s[1] to 1 to get string "11".It can be seen that the string "11"is beautiful because we can partition it into "11".It can be proven that 1is the minimum number of changes needed to make the string beautiful.
Example 3:
1
2
3
Input: s ="0000"Output: 0Explanation: We don't need to make any changes as the string "0000"is beautiful already.
classSolution {
publicintminChanges(String s) {
int changes = 0;
for (int i = 0; i < s.length(); i += 2) {
if (s.charAt(i) != s.charAt(i + 1)) {
changes++;
}
}
return changes;
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
defminChanges(self, s: str) -> int:
changes: int =0 n: int = len(s)
for i in range(0, n, 2):
if i +1< n and s[i] != s[i +1]:
changes +=1return changes