Maximum Score After Splitting a String

Maximum Score After Splitting a String

Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring).

The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring.

LeetCode Problem - 1422

class Solution {
    public int maxScore(String s) {
        int answer = 0;
        for(int i=0; i<s.length(); i++){
            int temp = splitChecker(i, s);
            answer = Math.max(answer, temp);
        }
        return answer;
    }

    public int splitChecker(int k, String str){
        int zeroCount = 0;
        for(int i=0; i<=k; i++){
            if(str.charAt(i) == '0') zeroCount++;
        }
        int oneCount = 0;
        for(int i=k; i<str.length(); i++){
            if(str.charAt(i) == '1') oneCount++;
        }

        if(zeroCount==0 || oneCount ==0){
            return (zeroCount+oneCount)-1;
        }
        return zeroCount+oneCount;
    }
}