# Find the direction from given string

Given a string of containing only L’s and R’s which represents left rotation and right rotation respectively. The task is to find the final direction of pivot(i.e N/E/S/W). Let a pivot is pointed towards north(N) in a compass.

Examples:

```Input: str = "LLRLRRL"
Output: W
In this input string we rotate pivot to left
when a L char is encountered and right when
R is encountered.

Input: str = "LL"
Output: S

```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Use a counter that incremented on seeing R and decremented on seeing L.
2. Finally use modulo on counter to get the direction.
3. If count is negative then directions will be different.Check the code for negative as well.

Below is the implementation of above approach:

## C++

 `// CPP implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the final direction ` `string findDirection(string s) ` `{ ` `    ``int` `count = 0; ` `    ``string d = ``""``; ` ` `  `    ``for` `(``int` `i = 0; i < s.length(); i++)  ` `    ``{ ` ` `  `        ``if` `(s == ``'\n'``) ` `            ``return` `NULL; ` ` `  `        ``if` `(s[i] == ``'L'``) ` `            ``count--; ` `        ``else`  `        ``{ ` `            ``if` `(s[i] == ``'R'``) ` `                ``count++; ` `        ``} ` `    ``} ` ` `  `    ``// if count is positive that implies ` `    ``// resultant is clockwise direction ` `    ``if` `(count > 0)  ` `    ``{ ` ` `  `        ``if` `(count % 4 == 0) ` `            ``d = ``"N"``; ` `        ``else` `if` `(count % 4 == 1) ` `            ``d = ``"E"``; ` `        ``else` `if` `(count % 4 == 2) ` `            ``d = ``"S"``; ` `        ``else` `if` `(count % 4 == 3) ` `            ``d = ``"W"``; ` `    ``} ` ` `  `    ``// if count is negative that implies ` `    ``// resultant is anti-clockwise direction ` `    ``if` `(count < 0)  ` `    ``{ ` ` `  `        ``if` `(count % 4 == 0) ` `            ``d = ``"N"``; ` `        ``else` `if` `(count % 4 == -1) ` `            ``d = ``"W"``; ` `        ``else` `if` `(count % 4 == -2) ` `            ``d = ``"S"``; ` `        ``else` `if` `(count % 4 == -3) ` `            ``d = ``"E"``; ` `    ``} ` `    ``return` `d; ` `} ` ` `  `    ``// Driver code ` `    ``int` `main() ` `    ``{ ` `        ``string s = ``"LLRLRRL"``; ` `        ``cout << (findDirection(s)) << endl; ` ` `  `        ``s = ``"LL"``; ` `        ``cout << (findDirection(s)) << endl; ` `    ``} ` ` `  `// This code is contributed by  ` `// SURENDRA_GANGWAR `

## Java

 `// Java implementation of above approach ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find the final direction ` `    ``static` `String findDirection(String s) ` `    ``{ ` `        ``int` `count = ``0``; ` `        ``String d = ``""``; ` ` `  `        ``for` `(``int` `i = ``0``; i < s.length(); i++) { ` ` `  `            ``if` `(s.charAt(``0``) == ``'\n'``) ` `                ``return` `null``; ` ` `  `            ``if` `(s.charAt(i) == ``'L'``) ` `                ``count--; ` `            ``else` `{ ` `                ``if` `(s.charAt(i) == ``'R'``) ` `                    ``count++; ` `            ``} ` `        ``} ` ` `  `        ``// if count is positive that implies ` `        ``// resultant is clockwise direction ` `        ``if` `(count > ``0``) { ` ` `  `            ``if` `(count % ``4` `== ``0``) ` `                ``d = ``"N"``; ` `            ``else` `if` `(count % ``4` `== ``1``) ` `                ``d = ``"E"``; ` `            ``else` `if` `(count % ``4` `== ``2``) ` `                ``d = ``"S"``; ` `            ``else` `if` `(count % ``4` `== ``3``) ` `                ``d = ``"W"``; ` `        ``} ` ` `  `        ``// if count is negative that implies ` `        ``// resultant is anti-clockwise direction ` `        ``if` `(count < ``0``) { ` ` `  `            ``if` `(count % ``4` `== ``0``) ` `                ``d = ``"N"``; ` `            ``else` `if` `(count % ``4` `== -``1``) ` `                ``d = ``"W"``; ` `            ``else` `if` `(count % ``4` `== -``2``) ` `                ``d = ``"S"``; ` `            ``else` `if` `(count % ``4` `== -``3``) ` `                ``d = ``"E"``; ` `        ``} ` `        ``return` `d; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String s = ``"LLRLRRL"``; ` `        ``System.out.println(findDirection(s)); ` ` `  `        ``s = ``"LL"``; ` `        ``System.out.println(findDirection(s)); ` `    ``} ` `} `

## C#

 `// C# implementation of above approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to find the final direction ` `static` `String findDirection(String s) ` `{ ` `    ``int` `count = 0; ` `    ``String d = ``""``; ` ` `  `    ``for` `(``int` `i = 0; i < s.Length; i++) ` `    ``{ ` ` `  `        ``if` `(s == ``'\n'``) ` `            ``return` `null``; ` ` `  `        ``if` `(s[i] == ``'L'``) ` `            ``count--; ` `        ``else` `{ ` `            ``if` `(s[i] == ``'R'``) ` `                ``count++; ` `        ``} ` `    ``} ` ` `  `    ``// if count is positive that implies ` `    ``// resultant is clockwise direction ` `    ``if` `(count > 0) ` `    ``{ ` ` `  `        ``if` `(count % 4 == 0) ` `            ``d = ``"N"``; ` `        ``else` `if` `(count % 4 == 1) ` `            ``d = ``"E"``; ` `        ``else` `if` `(count % 4 == 2) ` `            ``d = ``"S"``; ` `        ``else` `if` `(count % 4 == 3) ` `            ``d = ``"W"``; ` `    ``} ` ` `  `    ``// if count is negative that implies ` `    ``// resultant is anti-clockwise direction ` `    ``if` `(count < 0) ` `    ``{ ` ` `  `        ``if` `(count % 4 == 0) ` `            ``d = ``"N"``; ` `        ``else` `if` `(count % 4 == -1) ` `            ``d = ``"W"``; ` `        ``else` `if` `(count % 4 == -2) ` `            ``d = ``"S"``; ` `        ``else` `if` `(count % 4 == -3) ` `            ``d = ``"E"``; ` `    ``} ` `    ``return` `d; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``String s = ``"LLRLRRL"``; ` `    ``Console.WriteLine(findDirection(s)); ` ` `  `    ``s = ``"LL"``; ` `    ``Console.WriteLine(findDirection(s)); ` `} ` `} ` ` `  `// This code is contributed by Shashank `

## PHP

 ` 0)  ` `    ``{ ` `        ``if` `(``\$count` `% 4 == 0) ` `            ``\$d` `= ``"N"``; ` `        ``else` `if` `(``\$count` `% 4 == 1) ` `            ``\$d` `= ``"E"``; ` `        ``else` `if` `(``\$count` `% 4 == 2) ` `            ``\$d` `= ``"S"``; ` `        ``else` `if` `(``\$count` `% 4 == 3) ` `            ``\$d` `= ``"W"``; ` `    ``} ` `     `  `    ``// if count is negative that  ` `    ``// implies resultant is  ` `    ``// anti-clockwise direction ` `    ``if` `(``\$count` `< 0)  ` `    ``{ ` `        ``if` `(``\$count` `% 4 == 0) ` `            ``\$d` `= ``"N"``; ` `        ``else` `if` `(``\$count` `% 4 == -1) ` `            ``\$d` `= ``"W"``; ` `        ``else` `if` `(``\$count` `% 4 == -2) ` `            ``\$d` `= ``"S"``; ` `        ``else` `if` `(``\$count` `% 4 == -3) ` `            ``\$d` `= ``"E"``; ` `    ``} ` `    ``return` `\$d``; ` `} ` ` `  `// Driver code ` `\$s` `= ``"LLRLRRL"``; ` `echo` `findDirection(``\$s``).``"\n"``; ` ` `  `\$s` `= ``"LL"``; ` `echo` `findDirection(``\$s``).``"\n"``; ` ` `  `// This code is contributed ` `// by ChitraNayal     ` `?> `

Output:

```W
S
``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.