mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
java: add “1422. Maximum Score After Splitting a String”
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
c2916fdf5f
commit
d207204281
1 changed files with 28 additions and 0 deletions
28
java/maximum-score-after-splitting-a-string.java
Normal file
28
java/maximum-score-after-splitting-a-string.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue