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.
Example 1:
Input: s = "011101" Output: 5 Explanation: All possible ways of splitting s into two non-empty substrings are: left = "0" and right = "11101", score = 1 + 4 = 5 left = "01" and right = "1101", score = 1 + 3 = 4 left = "011" and right = "101", score = 1 + 2 = 3 left = "0111" and right = "01", score = 1 + 1 = 2 left = "01110" and right = "1", score = 2 + 1 = 3
Example 2:
Input: s = "00111" Output: 5 Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5
Example 3:
Input: s = "1111" Output: 3
Constraints:
2 <= s.length <= 500
- The string
s
consists of characters'0'
and'1'
only.
Approach 01:
-
C++
-
Python
#include <string> #include <algorithm> using namespace std; class Solution { public: int maxScore(string binaryString) { int stringLength = binaryString.length(); int totalOnesInString = 0; // Total number of '1's in the string // Count the total number of '1's in the string for (char character : binaryString) { if (character == '1') { ++totalOnesInString; } } int leftZeroCount = 0, rightOneCount = totalOnesInString, maximumScore = 0; // Iterate up to the second last character to ensure both parts are non-empty for (int index = 0; index < stringLength - 1; ++index) { if (binaryString[index] == '0') { ++leftZeroCount; } else { --rightOneCount; } // Calculate the score and update the maximum score maximumScore = max(maximumScore, leftZeroCount + rightOneCount); } return maximumScore; } };
class Solution: def maxScore(self, binaryString: str) -> int: stringLength = len(binaryString) totalOnesInString = binaryString.count('1') # Total number of '1's in the string leftZeroCount = 0 rightOneCount = totalOnesInString maximumScore = 0 # Iterate up to the second last character to ensure both parts are non-empty for index in range(stringLength - 1): if binaryString[index] == '0': leftZeroCount += 1 else: rightOneCount -= 1 # Calculate the score and update the maximum score maximumScore = max(maximumScore, leftZeroCount + rightOneCount) return maximumScore
Time Complexity:
- Counting ‘1’s:
- We iterate through the entire string to count the total number of ‘1’s.
- This operation takes \( O(n) \), where \( n \) is the length of the string.
- Iterating to Calculate Scores:
- We traverse the string again, up to the second-last character, to calculate scores.
- This also takes \( O(n) \) time.
- Overall Time Complexity:
\( O(n) \), as the operations to count ‘1’s and calculate scores dominate the runtime.
Space Complexity:
- Auxiliary Variables:
- We use a constant amount of additional space for variables such as
leftZeroCount
,rightOneCount
, andmaximumScore
.
- We use a constant amount of additional space for variables such as
- Input Storage:
- The input string is provided as an argument and does not require extra space to copy or process.
- Overall Space Complexity:
\( O(1) \), as the algorithm only uses a fixed amount of space regardless of the input size.