text: Check if given string is a substring of string formed by repeated concatenation of z to a | C ++ program for the above approach ; Function checks if a given string is valid or not and prints the output ; Boolean flag variable to mark if given string is valid ; Traverse the given string ; If adjacent character differ by 1 ; If character ' a ' is followed by 4 ; Else flip the flag and break from the loop ; Output according to flag variable ; Driver Code ; Given string ; Function Call
code: #include <iostream> NEW_LINE using namespace std ; void checkInfinite ( string s ) { bool flag = 1 ; int N = s . length ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( s [ i ] == char ( int ( s [ i + 1 ] ) + 1 ) ) { continue ; } else if ( s [ i ] == ' a ' && s [ i + 1 ] == ' z ' ) { continue ; } else { flag = 0 ; break ; } } if ( flag == 0 ) cout << " NO " ; else cout << " YES " ; } int main ( ) { string s = " ecbaz " ; checkInfinite ( s ) ; return 0 ; }