Maximum Balanced String Partitions | Function to find a maximum number X , such that a given String can be partitioned into X subStrings that are each balanced ; If the size of the String is 0 , then answer is zero ; Variable that represents the number of ' R ' s and ' L ' s ; To store maximum number of possible partitions ; Increment the variable r if the character in the String is ' R ' ; Increment the variable l if the character in the String is ' L ' ; If r and l are equal , then increment ans ; Return the required answer ; Driver code ; Function call
function BalancedPartition ( str , n ) { if ( n == 0 ) return 0 ; let r = 0 , l = 0 ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { r ++ ; } else if ( str [ i ] == ' ' ) { l ++ ; } if ( r == l ) { ans ++ ; } } return ans ; } let str = " " ; let n = str . length ; document . write ( BalancedPartition ( str , n ) + " " ) ;