Check if a given string is made up of two alternating characters | C ++ implementation of the approach ; Function that returns true if the string is made up of two alternating characters ; Check if ith character matches with the character at index ( i + 2 ) ; If string consists of a single character repeating itself ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isTwoAlter ( string s ) { for ( int i = 0 ; i < s . length ( ) - 2 ; i ++ ) { if ( s [ i ] != s [ i + 2 ] ) { return false ; } } if ( s [ 0 ] == s [ 1 ] ) return false ; return true ; } int main ( ) { string str = " ABAB " ; if ( isTwoAlter ( str ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }