From d207204281e52b318c630034a9c8580e4d49443d Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 22 Dec 2023 17:42:07 +0100 Subject: [PATCH] =?UTF-8?q?java:=20add=20=E2=80=9C1422.=20Maximum=20Score?= =?UTF-8?q?=20After=20Splitting=20a=20String=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...aximum-score-after-splitting-a-string.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 java/maximum-score-after-splitting-a-string.java diff --git a/java/maximum-score-after-splitting-a-string.java b/java/maximum-score-after-splitting-a-string.java new file mode 100644 index 0000000..9d27a31 --- /dev/null +++ b/java/maximum-score-after-splitting-a-string.java @@ -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; + } +}