字符串修改最少次数计算
问题描述
小C有一个由数字字符组成的字符串,她想对这个字符串进行修改,使修改后的字符串中没有连续相同的字符。她需要计算至少进行多少次修改,才能确保字符串中的每两个连续字符不同。
测试样例
样例1:
输入:
s = "111222333"
输出:3
样例2:
输入:
s = "11551111"
输出:4
样例3:
输入:
s = "1234567890"
输出:0
思路
因为相邻有影响,那么我们考虑取相邻相同值的数量,对于一段长度为k的相同段,我们的最优解就是k/2;我们只需要隔一个一改即可;
code
#include <bits/stdc++.h>
using namespace std;
int solution(string s) {
int ans = 0;
int count = 1;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == s[i-1]){
count++;
}
else{
ans+=count/2;
count=1;
}
}
ans+=count/2;
//cout<<ans<<endl;
return ans;
}
int main() {
cout << (solution("111222333") == 3) << endl;
cout << (solution("11551111") == 4) << endl;
cout << (solution("1234567890") == 0) << endl;
return 0;
}