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
function matchPattern ( s ) { let count = 0 ; let n = s . length ; let i = 0 ; while ( i < n ) { while ( i < n && s [ i ] == ' ' ) { count ++ ; i ++ ; } while ( i < n && s [ i ] == ' ' ) { count -- ; i ++ ; } if ( count != 0 ) return false ; } return true ; } let s = " " ; if ( matchPattern ( s ) == true ) document . write ( " " ) ; else document . write ( " " ) ;