problem
1422. Maximum Score After Splitting a String
solution#1:
code
class Solution {
public:
int maxScore(string s)
{
int left = 0, right = 0;
for(auto c:s)
{
if(c=='1') right++;
}
int res = 0;
for(int i=0; i<s.size()-1; i++) // substr size more than one.
{
if(s[i]=='0') left++;
else right--;
res = max(res, left+right);
}
return res;
}
};
可以通过其中一个的计数,改变另一个的计数;
参考
1. leetcode_easy_string_1422. Maximum Score After Splitting a String;
完