Count all indices of cyclic regular parenthesis | Function to find all indices which cyclic shift leads to get balanced parenthesis ; Create auxiliary array ; Finding prefix sum and minimum element ; Update the minimum element ; Check if count of ' ( ' and ' ) ' are equal ; Find count of minimum element ; Find the frequency of mn ; Return the count ; Given string S ; length of the string S
function countCyclicShifts ( S , n ) { let aux = [ ] ; for ( let i = 0 ; i < n ; ++ i ) { if ( S [ i ] == ' ' ) aux [ i ] = 1 ; else aux [ i ] = - 1 ; } let mn = aux [ 0 ] ; for ( let i = 1 ; i < n ; ++ i ) { aux [ i ] += aux [ i - 1 ] ; mn = Math . min ( mn , aux [ i ] ) ; } if ( aux [ n - 1 ] != 0 ) return 0 ; let count = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( aux [ i ] == mn ) count ++ ; } return count ; } let S = " " ; let N = S . length ; document . write ( countCyclicShifts ( S , N ) ) ;