-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maximum Score After Splitting a String.cpp
62 lines (39 loc) · 1.24 KB
/
Maximum Score After Splitting a String.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution {
public:
int maxScore(string s) {
int result = 0 ;
for(int i = 0 ; i < s.size()-1 ; i ++){
string leftString = s.substr(0,i+1);
string rightString = s.substr(i+1);
char zero = '0';
char one = '1';
int zeroOccur = count(leftString.begin(), leftString.end(), zero);
int oneOccur = count(rightString.begin(), rightString.end(), one);
int curr = zeroOccur + oneOccur;
result = max(result, curr);
}
return result;
}
};
class Solution {
public:
int maxScore(string s) {
int total_ones = count(s.begin(), s.end(), '1');
int result = 0 ;
int ones = 0;
int zeroes = 0;
int n = s.length();
//n-2 index -> till then only we can split it into two strings
for(int i = 0 ; i < n-1; i ++){
if(s[i] == '1'){
ones++;
}
else{
zeroes++;
}
int right_ones = total_ones - ones;
result = max(result, right_ones + zeroes);
}
return result;
}
};