Last Updated: 8 Sep, 2025

Find the direction from given string

Easy
Asked in company
Microsoft

Problem statement

You are given a string str consisting only of the characters 'L' and 'R'. Imagine a pivot on a compass that is initially pointing North (N).

Your task is to determine the final direction of the pivot after performing a series of rotations as described by the input string.

  • 'L' represents a 90-degree left (counter-clockwise) rotation.

  • 'R' represents a 90-degree right (clockwise) rotation.

  • Input Format:
    A single line containing the string str.
    


    Output Format:
    Print a single character representing the final direction: 'N' for North, 'E' for East, 'S' for South, or 'W' for West.
    


    Note:
    The compass directions follow a cycle: North -> East -> South -> West -> North.
    
    This problem can be efficiently solved by mapping the directions to numbers (e.g., N=0, E=1, S=2, W=3) and using modular arithmetic to track the current state.