Skip to content

1422. Maximum Score After Splitting a String #1044

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can leverage the hint provided by precomputing a prefix sum of ones ('1') in the string. Here's how we can break down the solution:

Steps:

  1. Prefix sum of ones: Precompute an array where each element at index i contains the number of ones ('1') up to index i in the string.
  2. Iterate over the string: For each position i, treat the substring from 0 to i as the "left" substring and from i+1 to the end of the string as the "right" substring.
    • Count the zeros in the left substring by simply counting them as you iterate.
    • Use the prefix sum to count the ones in the right substring (by subtracting the prefix sum at the split point from the total count of ones in the string).
  3. Compute the score: …

Replies: 1 comment 2 replies

Comment options

mah-shamim
Jan 1, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jan 1, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants