# Check if it is possible to return to the starting position after moving in the given directions

Given a string S having N directions in which a person travels. The task is to check if he/she will be able to return to the same place where he/she started. On Day i(1 <= i <= N), he will travel a positive distance in the following direction:

North if the i-th letter of str is N

West if the i-th letter of str is W

South if the i-th letter of str is S

East if the i-th letter of str is E

If he can return back to the place where he starts after nth day, print “YES” else print “NO”.

**Examples:**

Input:str = “NNNWEWESSS”

Output:YES

On the 1st, 2nd, and 3rd day he goes to north and on the 4th day he goes west, then eventually

returns where he was standing on the 3rd day on the 5th day, then on the 6th day he again goes to

west.On the 7th day he again return exactly to where he was standing on the 5th day.And on the

10th day he returns home safely.

Input:str = “NW”

Output:NO

**Approach:** There has to be a same number of N as there are a number of S and also the same number of E as there is W. So, count each type of directions given and just check if they are equal or not.

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `main() ` ` ` `{ ` ` ` `string st = ` `"NNNWEWESSS"` `; ` ` ` `int` `len = st.length(); ` ` ` ` ` `int` `n = 0 ; ` `// Count of North ` ` ` `int` `s = 0 ; ` `// Count of South ` ` ` `int` `e = 0 ; ` `// Count of East ` ` ` `int` `w = 0 ; ` `// Count of West ` ` ` ` ` `for` `(` `int` `i = 0; i < len ; i++ ) ` ` ` `{ ` ` ` `if` `(st[i]==` `'N'` `) ` ` ` `n += 1; ` ` ` `if` `(st[i] == ` `'S'` `) ` ` ` `s += 1; ` ` ` `if` `(st[i] == ` `'W'` `) ` ` ` `w+= 1 ; ` ` ` `if` `(st[i] == ` `'E'` `) ` ` ` `e+= 1 ; ` ` ` `} ` ` ` ` ` `if` `(n == s && w == e) ` ` ` `cout<<(` `"YES"` `)<<endl; ` ` ` `else` ` ` `cout<<(` `"NO"` `)<<endl; ` ` ` ` ` `} ` ` ` `// This code is contributed by ` ` ` `// Sahil_Shelangia ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` ` ` `public` `class` `GFG { ` ` ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `String st = ` `"NNNWEWESSS"` `; ` ` ` `int` `len = st.length(); ` ` ` ` ` `int` `n = ` `0` `; ` `// Count of North ` ` ` `int` `s = ` `0` `; ` `// Count of South ` ` ` `int` `e = ` `0` `; ` `// Count of East ` ` ` `int` `w = ` `0` `; ` `// Count of West ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < len ; i++ ) ` ` ` `{ ` ` ` `if` `(st.charAt(i)==` `'N'` `) ` ` ` `n+= ` `1` `; ` ` ` `if` `(st.charAt(i) == ` `'S'` `) ` ` ` `s+= ` `1` `; ` ` ` `if` `(st.charAt(i) == ` `'W'` `) ` ` ` `w+= ` `1` `; ` ` ` `if` `(st.charAt(i) == ` `'E'` `) ` ` ` `e+= ` `1` `; ` ` ` `} ` ` ` `if` `(n == s && w == e) ` ` ` `System.out.println(` `"YES"` `); ` ` ` `else` ` ` `System.out.println(` `"NO"` `) ; ` ` ` ` ` `} ` ` ` `// This Code is contributed by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python implementation of above approach ` ` ` `st ` `=` `"NNNWEWESSS"` `length ` `=` `len` `(st) ` `n ` `=` `0` `# Count of North ` `s ` `=` `0` `# Count of South ` `e ` `=` `0` `# Count of East ` `w ` `=` `0` `# Count of West ` `for` `i ` `in` `range` `(length): ` ` ` `if` `(st[i]` `=` `=` `"N"` `): ` ` ` `n` `+` `=` `1` ` ` `if` `(st[i]` `=` `=` `"S"` `): ` ` ` `s` `+` `=` `1` ` ` `if` `(st[i]` `=` `=` `"W"` `): ` ` ` `w` `+` `=` `1` ` ` `if` `(st[i]` `=` `=` `"E"` `): ` ` ` `e` `+` `=` `1` `if` `(n ` `=` `=` `s ` `and` `w ` `=` `=` `e): ` ` ` `print` `(` `"YES"` `) ` `else` `: ` ` ` `print` `(` `"NO"` `) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Main Method ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `string` `st = ` `"NNNWEWESSS"` `; ` ` ` `int` `len = st.Length; ` ` ` ` ` `int` `n = 0 ; ` `// Count of North ` ` ` `int` `s = 0 ; ` `// Count of South ` ` ` `int` `e = 0 ; ` `// Count of East ` ` ` `int` `w = 0 ; ` `// Count of West ` ` ` ` ` `for` `(` `int` `i = 0; i < len ; i++ ) ` ` ` `{ ` ` ` `if` `(st[i]==` `'N'` `) ` ` ` `n += 1 ; ` ` ` `if` `(st[i] == ` `'S'` `) ` ` ` `s += 1 ; ` ` ` `if` `(st[i] == ` `'W'` `) ` ` ` `w += 1 ; ` ` ` `if` `(st[i] == ` `'E'` `) ` ` ` `e += 1 ; ` ` ` `} ` ` ` ` ` `if` `(n == s && w == e) ` ` ` `Console.WriteLine(` `"YES"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"NO"` `) ; ` ` ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by Subhadeep ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

YES

## Recommended Posts:

- Distributing M items in a circle of size N starting from K-th position
- Check if two people starting from different points ever meet
- Program to find simple moving average
- Maximum possible intersection by moving centers of line segments
- Time until distance gets equal to X between two objects moving in opposite direction
- Minimum time to return array to its original state after given modifications
- Minimum steps to come back to starting point in a circular tour
- Insert string at specified position in PHP
- Position of n among the numbers made of 2, 3, 5 & 7
- How to position a div at the bottom of its container using CSS?
- Find the position of box which occupies the given ball
- Find the value at kth position in the generated array
- Final cell position in the matrix
- Position of a person diametrically opposite on a circle
- Find position of the given number among the numbers made of 4 and 7

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.