Robot Return to Origin

easy string simulation

Problem

A robot starts at the origin (0, 0). Given a string of moves ('U', 'D', 'L', 'R'), return true if and only if the robot ends at (0, 0).

Inputmoves = "UD"
Outputtrue
Up then Down lands back at the origin.

def judge_circle(moves):
    x = y = 0
    for ch in moves:
        if   ch == 'U': y += 1
        elif ch == 'D': y -= 1
        elif ch == 'L': x -= 1
        elif ch == 'R': x += 1
    return x == 0 and y == 0
function judgeCircle(moves) {
  let x = 0, y = 0;
  for (const ch of moves) {
    if      (ch === 'U') y++;
    else if (ch === 'D') y--;
    else if (ch === 'L') x--;
    else if (ch === 'R') x++;
  }
  return x === 0 && y === 0;
}
class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0, y = 0;
        for (char ch : moves.toCharArray()) {
            if      (ch == 'U') y++;
            else if (ch == 'D') y--;
            else if (ch == 'L') x--;
            else if (ch == 'R') x++;
        }
        return x == 0 && y == 0;
    }
}
bool judgeCircle(string moves) {
    int x = 0, y = 0;
    for (char ch : moves) {
        if      (ch == 'U') y++;
        else if (ch == 'D') y--;
        else if (ch == 'L') x--;
        else if (ch == 'R') x++;
    }
    return x == 0 && y == 0;
}
Time: O(n) Space: O(1)