-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathjudge_circle.py
More file actions
38 lines (29 loc) · 932 Bytes
/
judge_circle.py
File metadata and controls
38 lines (29 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Judge Route Circle
Given a sequence of robot moves (R, L, U, D), determine whether the robot
returns to its starting position after completing all moves.
Reference: https://leetcode.com/problems/robot-return-to-origin/
Complexity:
Time: O(n) where n is the number of moves
Space: O(1)
"""
from __future__ import annotations
def judge_circle(moves: str) -> bool:
"""Determine whether a sequence of moves returns to the origin.
Args:
moves: A string of move characters ('U', 'D', 'L', 'R').
Returns:
True if the robot ends at the starting position, False otherwise.
Examples:
>>> judge_circle("UD")
True
"""
move_counts = {
"U": 0,
"D": 0,
"R": 0,
"L": 0,
}
for char in moves:
move_counts[char] = move_counts[char] + 1
return move_counts["L"] == move_counts["R"] and move_counts["U"] == move_counts["D"]