Check if the given string is linear or not | C ++ program for the above approach ; Function to check if the given string is linear or not ; Iterate over string ; If character is not same as the first character then return false ; Increment the tmp ; Driver Code ; Given String str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int is_linear ( string s ) { int tmp = 0 ; char first = s [ 0 ] ; for ( int pos = 0 ; pos < s . length ( ) ; pos += tmp ) { if ( s [ pos ] != first ) { return false ; } tmp ++ ; } return true ; } int main ( ) { string str = " aapaxyayziabcde " ; if ( is_linear ( str ) ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } return 0 ; }