A bike courier begins every shift at the depot, which sits at the center of the city's delivery grid. The dispatch app logs each move the courier makes as a single character: 'N' for one block north, 'S' for one block south, 'E' for one block east, or 'W' for one block west. At the end of the shift, the courier's manager wants to confirm the bike was returned to the depot rather than left somewhere in the city. Given the full log of moves for one shift, determine whether the courier ends exactly where they started.
A single line containing a string s made up only of the characters N, S, E, and W, representing the moves in the order they were made.
Print YES if performing every move in order brings the courier back to the depot (its starting position), otherwise print NO.
1 <= length of s <= 20000 s consists only of the uppercase letters N, S, E, W
Example 1
Input
NESW
Expected
YES
Explanation
One move north, one east, one south, and one west exactly cancel each other out (north cancels south, east cancels west), so the courier ends with zero net displacement and is back at the depot: YES.
Example 2
Input
NNEE
Expected
NO
Explanation
Two moves north and two moves east push the courier to a point two blocks north and two blocks east of the depot. That is not the starting position, so the answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →