Final state of the string after modification | Function to return final positions of the boxes ; Populate forces going from left to right ; Populate forces going from right to left ; return final state of boxes ; Driver code ; Function call to print answer
def pushBoxes ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE force = [ 0 ] * N NEW_LINE f = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if S [ i ] == ' R ' : NEW_LINE INDENT f = N NEW_LINE DEDENT elif S [ i ] == ' L ' : NEW_LINE INDENT f = 0 NEW_LINE DEDENT else : NEW_LINE INDENT f = max ( f - 1 , 0 ) NEW_LINE DEDENT force [ i ] += f NEW_LINE DEDENT f = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if S [ i ] == ' L ' : NEW_LINE INDENT f = N NEW_LINE DEDENT elif S [ i ] == ' R ' : NEW_LINE INDENT f = 0 NEW_LINE DEDENT else : NEW_LINE INDENT f = max ( f - 1 , 0 ) NEW_LINE DEDENT force [ i ] -= f NEW_LINE DEDENT return " " . join ( ' . ' if f == 0 else ' R ' if f > 0 else ' L ' for f in force ) NEW_LINE DEDENT S = " . L . R . . . LR . . L . . " NEW_LINE print ( pushBoxes ( S ) ) NEW_LINE