Generate an alternate increasing and decreasing Array | Function that returns generated array ; Dynamically allocate array ; START , END = 0 , 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 code
def DiStirngMatch ( Str ) : NEW_LINE INDENT N = len ( Str ) NEW_LINE arr = ( N + 1 ) * [ 0 ] NEW_LINE START , END = 0 , N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( Str [ i ] == ' I ' ) : NEW_LINE INDENT arr [ i ] = START NEW_LINE START += 1 NEW_LINE DEDENT if ( Str [ i ] == ' D ' ) : NEW_LINE INDENT arr [ i ] = END NEW_LINE END -= 1 NEW_LINE DEDENT DEDENT arr [ N ] = START NEW_LINE return arr NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Str = " IDID " NEW_LINE N = len ( Str ) NEW_LINE ptr = DiStirngMatch ( Str ) NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT print ( ptr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT