-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotLab.java
More file actions
111 lines (77 loc) · 2.75 KB
/
RobotLab.java
File metadata and controls
111 lines (77 loc) · 2.75 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*n a magical land, there is a young explorer named Raju who embarks on a thrilling adventure with his enchanted vehicle. This magical vehicle can only move in four directions: right (R), left (L), up (U), and down (D). Raju starts his journey at the heart of the mystical forest, located at coordinates (0, 0).
As Raju follows a sequence of mysterious symbols etched on ancient stones, he maneuvers his magical vehicle accordingly. 'R' guides him to the right, 'L' to the left, 'U' upwards, and 'D' downwards. The direction he faces does not matter; the symbols' magic ensures his vehicle moves in the intended direction.
Your mission is to help Raju determine whether he will return to the enchanted forest's center (0,0) after completing all his moves. If he successfully navigates back to the starting point, the forest's magic will protect him, and he will be able to continue his adventure. If not, he might be lost in the mystical realm forever.
Can you assist Raju in deciphering his fate? If he ends up back at the starting point, please say YES; otherwise, say NO.
Input Format
The first line of the input contains an integer n — the size of string moves
The second line contains the string moves
Output Format
Raju needs to print ”YES” if car returns to origin, else “NO”.
Constraints
1 <= moves.length <= 2 * 104
moves only contains the characters 'U', 'D', 'L' and 'R'.
Sample Testcase 0
Testcase Input
6
UUDDLR
Testcase Output
YES
Explanation
The robot moves:
From (0,0) to (1,0)
From (1,0) to (2,0)
From (2,0) to (1,0)
From (1,0) to (0,0)
From (0,0) to (-1,0)
From (-1,0) to (0,0)
After these moves, the robot ends up back at the origin (0, 0).
Sample Testcase 1
Testcase Input
5
UDLRL
Testcase Output
NO
Explanation
In this scenario, the robot moves:
From (0,0) to (1,0)
From (1,0) to (0,0)
From (0,0) to (-1,0)
From (-1,0) to (0,0)
From (0,0) to (-1,0)
After these moves, the robot does not return to the origin (0, 0). Therefore, the output is false.*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution {
public static boolean check(String s)
{
int x=0,y=0;
for(char c:s.toCharArray())
{
if(c=='U' || c=='R')
{
x++;
}
else if(c=='D' || c=='L')
{
x--;
}
}
return x==0&&y==0;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String str=sc.nextLine();
if(check(str))
{
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}