#K42957. Check Guard Return to Origin
Check Guard Return to Origin
Check Guard Return to Origin
You are given a string representing the movements of a guard on a two-dimensional grid. Each character of the string is one of N
, S
, E
, or W
. The guard starts at the origin (0,0) and moves one unit in the respective direction for each character.
The guard returns to the starting point if and only if the net displacement in both directions is zero. In mathematical terms, let \( \Delta y \) be the net movement in the north-south direction and \( \Delta x \) the net movement in the east-west direction. The guard returns if and only if:
$$\Delta y = 0 \quad \text{and} \quad \Delta x = 0.$$
Print YES
if the guard returns to the starting point; otherwise, print NO
.
inputFormat
The input consists of a single line containing a string path comprised solely of the characters N
, S
, E
, and W
, representing the movements of the guard.
outputFormat
Output a single line: YES
if the guard returns to the origin (0,0) after completing the given path, and NO
otherwise.
NESW
YES