LeetCode/java/maximum-score-after-splitting-a-string.java
Matej Focko f0cdb67ac1
chore(java): format
Signed-off-by: Matej Focko <mfocko@redhat.com>
2024-03-02 21:01:53 +01:00

28 lines
559 B
Java

class Solution {
public int maxScore(String s) {
// count the ones
int ones = 0;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) == '1') {
++ones;
}
}
int zeros = s.charAt(0) == '0' ? 1 : 0;
int foundScore = ones + zeros;
for (int i = 1; i < s.length() - 1; ++i) {
switch (s.charAt(i)) {
case '0':
++zeros;
break;
case '1':
--ones;
break;
}
foundScore = Math.max(foundScore, ones + zeros);
}
return foundScore;
}
}