#C2458. Return to Origin: Minimum Moves Validation
Return to Origin: Minimum Moves Validation
Return to Origin: Minimum Moves Validation
In this problem, you are given a string S consisting solely of the characters 'U', 'D', 'L', and 'R'. Each character represents a single move by a robot in the up, down, left, or right direction respectively. The robot starts at the origin (0,0). Your task is to determine if the entire sequence of moves brings the robot back to the origin. If it does, output the total number of moves; otherwise, output -1.
Formally, let (S) be a sequence of moves. The robot's position changes as follows: for each character in (S), update the position accordingly. If after processing all characters the position is ((0,0)), the answer is (|S|) (the length of the string). Otherwise, the answer is (-1).
inputFormat
Input is read from standard input. It consists of a single line containing a non-negative string (S) representing the sequence of moves (only characters 'U', 'D', 'L', and 'R'). An empty string is allowed.
outputFormat
Output a single integer to standard output. If the robot returns to the origin after performing all moves, output the length of the string (S). Otherwise, output (-1).## sample
UUDDLRLR
8