Generate an alternate increasing and decreasing Array | Function that returns generated array ; Dynamically allocate array ; START = 0 , END = N ; iterate over array ; if Str [ i ] == ' I ' assign arr [ i ] as START and increment START ; if str [ i ] == ' D ' assign arr [ i ] as END and decrement END ; assign A [ N ] as START ; return starting address of array A ; Driver Program
function DiStirngMatch ( Str ) { var N = Str . length ; var arr = Array ( N ) . fill ( 0 ) ; var START = 0 , END = N ; for ( var i = 0 ; i < N ; i ++ ) { if ( Str [ i ] == ' ' ) arr [ i ] = START ++ ; if ( Str [ i ] == ' ' ) arr [ i ] = END -- ; } arr [ N ] = START ; return arr ; } var Str = " " ; var N = Str . length ; var ptr = DiStirngMatch ( Str ) ; for ( var i = 0 ; i <= N ; i ++ ) document . write ( ptr [ i ] + " " ) ;