检查二进制字符串在C ++中是否包含连续的相同字符串
假设我们有一个二进制字符串。我们的任务是检查字符串是否具有连续的相同字符。如果连续存在相同的字符,则该字符无效,否则有效。那么字符串“101010”有效,但“10111010”无效。
为了解决这个问题,我们将从左向右遍历,如果两个连续字符相同,则返回false,否则返回true。
示例
#include <iostream>
#include <algorithm>
using namespace std;
bool isConsecutiveSame(string str){
int len = str.length();
for(int i = 0; i<len - 1; i++){
if(str[i] == str[i + 1])
return false;
}
return true;
}
int main() {
string str = "101010";
if(isConsecutiveSame(str))
cout << "No consecutive same characters";
else
cout << "Consecutive same characters found";
}输出结果
No consecutive same characters
热门推荐
10 结婚婚宴圆满祝福语简短
11 结婚词祝福语简短精辟
12 孩子出院上班祝福语简短
13 古诗辞职祝福语大全简短
14 兄弟告别的祝福语简短
15 送你妹妹新婚祝福语简短
16 名人写信结尾祝福语简短
17 如何回应老师祝福语简短
18 幼师辞职道别祝福语简短