Check if a string follows a ^ nb ^ n pattern or not | C ++ code to check a ^ nb ^ n pattern ; Returns " Yes " str is of the form a ^ nb ^ n . ; check first half is ' a ' and other half is full of ' b ' ; Driver code ; Function call
#include <iostream> NEW_LINE using namespace std ; string isAnBn ( string str ) { int n = str . length ( ) ; if ( n & 1 ) return " No " ; int i ; for ( i = 0 ; i < n / 2 ; i ++ ) if ( str [ i ] != ' a ' str [ n - i - 1 ] != ' b ' ) return " No " ; return " Yes " ; } int main ( ) { string str = " ab " ; cout << isAnBn ( str ) ; return 0 ; }