Skip to content

Commit dd38ab3

Browse files
chore: add LeetCode daily solution
1 parent e0e46b7 commit dd38ab3

5 files changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Robot Return to Origin (Easy)
2+
3+
**Problem ID:** 657
4+
**Date:** 2026-04-05
5+
**Link:** https://leetcode.com/problems/robot-return-to-origin/
6+
7+
## Approach
8+
9+
To solve the "Robot Return to Origin" problem, we can use a straightforward counting approach based on the moves the robot makes. The main idea is to track the net effect of the moves in the vertical and horizontal directions.
10+
11+
### Approach:
12+
13+
1. **Initialization**: Start with two counters, `vertical` and `horizontal`, both initialized to zero. These will represent the net movements in the y-axis and x-axis, respectively.
14+
15+
2. **Iterate through moves**: Loop through each character in the input string `moves`:
16+
- If the move is 'U' (up), increment the `vertical` counter by 1.
17+
- If the move is 'D' (down), decrement the `vertical` counter by 1.
18+
- If the move is 'L' (left), decrement the `horizontal` counter by 1.
19+
- If the move is 'R' (right), increment the `horizontal` counter by 1.
20+
21+
3. **Check final position**: After processing all moves, check if both `vertical` and `horizontal` counters are zero. If they are, it means the robot has returned to the origin (0, 0), and we return `true`. Otherwise, return `false`.
22+
23+
### Data Structures:
24+
- We only need a few integer variables (`vertical` and `horizontal`), which means the space complexity is O(1).
25+
26+
### Complexity:
27+
- **Time Complexity**: O(n), where n is the length of the `moves` string. We need to iterate through the string once.
28+
- **Space Complexity**: O(1), since we are using a constant amount of space regardless of the input size.
29+
30+
This approach efficiently determines if the robot returns to the origin by simply counting the net movements in each direction, making it optimal for the problem constraints.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public boolean judgeCircle(String moves) {
3+
int horizontal = 0;
4+
int vertical = 0;
5+
6+
for (char move : moves.toCharArray()) {
7+
switch (move) {
8+
case 'U':
9+
vertical++;
10+
break;
11+
case 'D':
12+
vertical--;
13+
break;
14+
case 'L':
15+
horizontal--;
16+
break;
17+
case 'R':
18+
horizontal++;
19+
break;
20+
}
21+
}
22+
23+
return horizontal == 0 && vertical == 0;
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var judgeCircle = function(moves) {
2+
let x = 0, y = 0;
3+
for (let move of moves) {
4+
if (move === 'U') y++;
5+
else if (move === 'D') y--;
6+
else if (move === 'L') x--;
7+
else if (move === 'R') x++;
8+
}
9+
return x === 0 && y === 0;
10+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def judgeCircle(self, moves: str) -> bool:
3+
return moves.count('U') == moves.count('D') and moves.count('L') == moves.count('R')

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
414414
- 2026-04-02 — [Maximum Amount of Money Robot Can Earn](https://leetcode.com/problems/maximum-amount-of-money-robot-can-earn/) (Medium) → `Medium/2026-04-02-3418-Maximum-Amount-of-Money-Robot-Can-Earn`
415415
- 2026-04-03 — [Maximum Walls Destroyed by Robots](https://leetcode.com/problems/maximum-walls-destroyed-by-robots/) (Hard) → `Hard/2026-04-03-3661-Maximum-Walls-Destroyed-by-Robots`
416416
- 2026-04-04 — [Decode the Slanted Ciphertext](https://leetcode.com/problems/decode-the-slanted-ciphertext/) (Medium) → `Medium/2026-04-04-2075-Decode-the-Slanted-Ciphertext`
417+
- 2026-04-05 — [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) (Easy) → `Easy/2026-04-05-657-Robot-Return-to-Origin`

0 commit comments

Comments
 (0)