Return to Origin

STRING

Problem

Given a string moves representing the move sequence made by a sailboat, where each character in the string corresponds to a move in one of the four cardinal directions ('R' for right, 'L' for left, 'U' for up, and 'D' for down), determine if these moves will bring the sailboat back to its original position, i.e., the origin (0,0).

Examples

judgeCircle("RLUD") // returns true // The sailboat makes one move each to // the right, left, upward, and downward, // ending up at the origin. judgeCircle("UUDDLRLR") // returns true // The sailboat makes balanced moves in all directions, // ultimately returning to the origin.
Loading...