diff --git a/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.java b/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.java new file mode 100644 index 0000000..6a0e8d8 --- /dev/null +++ b/1422-maximum-score-after-splitting-a-string/1422-maximum-score-after-splitting-a-string.java @@ -0,0 +1,16 @@ +class Solution { + public int maxScore(String s) { + int ones = 0; + int current = s.charAt(0) == '0' ? 1 : 0; + int score = current; + for (int i = 1; i < s.length() - 1; i++) { + if (s.charAt(i) == '0') current++; + else { + ones++; + current--; + } + score = Math.max(score, current); + } + return ones + score + (s.charAt(s.length() - 1) == '1' ? 1 : 0); + } +} \ No newline at end of file