MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / maxScore

Method maxScore

MaximumScoreAfterSplittingAString.java:3–19  ·  view source on GitHub ↗
(String s)

Source from the content-addressed store, hash-verified

1// two pass
2class Solution {
3 public int maxScore(String s) {
4 //find all the ones
5 int ones = 0;
6 int n = s.length();
7 for(int i=0;i<n;i++){
8 if(s.charAt(i) == '1') ones++;
9 }
10 // find max partition
11 int res = 0;
12 int zeros=0;
13 for(int i=0;i<n-1;i++){
14 if(s.charAt(i) == '0') zeros++;
15 else ones--;
16 res = Math.max(res, ones + zeros);
17 }
18 return res;
19 }
20}
21
22// single pass

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected