Check If every group of a ' s ▁ is ▁ followed ▁ by ▁ a ▁ group ▁ of ▁ b ' s of same length | Function to match whether there are always n consecutive b ' s ▁ followed ▁ by ▁ n ▁ consecutive ▁ a ' s throughout the string ; Traverse through the string ; Count a 's in current segment ; Count b 's in current segment ; If both counts are not same . ; Driver code
< ? php function matchPattern ( $ s ) { $ count = 0 ; $ n = strlen ( $ s ) ; $ i = 0 ; while ( $ i < $ n ) { while ( $ i < $ n && $ s [ $ i ] == ' a ' ) { $ count ++ ; $ i ++ ; } while ( $ i < $ n && $ s [ $ i ] == ' b ' ) { $ count -- ; $ i ++ ; } if ( $ count != 0 ) return false ; } return true ; } $ s = " bb " ; if ( matchPattern ( $ s ) == true ) echo " Yes " ; else echo " No " ; ? >