Maximize absolute displacement from origin by moving on X | Recursive function to find the maximum absolute displacement from origin by performing the given set of moves ; If i is equal to N ; If S [ i ] is equal to 'L ; If S [ i ] is equal to 'R ; If S [ i ] is equal to '? ; Function to find the maximum absolute displacement from the origin ; Return the maximum absolute displacement ; Input ; Function call
def DistRecursion ( S , i , dist ) : NEW_LINE INDENT if i == len ( S ) : NEW_LINE INDENT return abs ( dist ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if S [ i ] == ' L ' : NEW_LINE INDENT return DistRecursion ( S , i + 1 , dist - 1 ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if S [ i ] == ' R ' : NEW_LINE INDENT return DistRecursion ( S , i + 1 , dist + 1 ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT return max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) NEW_LINE DEDENT def maxDistance ( S ) : NEW_LINE INDENT return DistRecursion ( S , 0 , 0 ) NEW_LINE DEDENT S = " ? RRR ? " NEW_LINE print ( maxDistance ( S ) ) NEW_LINE