Following Directions (Codeforces 1791B)
Tags: Implement, geometry

Alperen is standing at the point $$$(0,0)$$$. He is given a string $$$s$$$ of length $$$n$$$ and performs $$$n$$$ moves. The $$$i$$$-th move is as follows:   if $$$s_i = \texttt{L}$$$, then move one unit left;  if $$$s_i = \texttt{R}$$$, then move one unit right;  if $$$s_i = \texttt{U}$$$, then move one unit up;  if $$$s_i = \texttt{D}$$$, then move one unit down.    If Alperen starts at the center point, he can make the four moves shown.  There is a candy at $$$(1,1)$$$ (that is, one unit above and one unit to the right of Alperen's starting point). You need to determine if Alperen ever passes the candy.   Alperen's path in the first test case. 

Input: The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of testcases.The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 50$$$) — the length of the string.The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of characters $$$\texttt{L}$$$, $$$\texttt{R}$$$, $$$\texttt{D}$$$, and $$$\texttt{U}$$$, denoting the moves Alperen makes.

Output: For each test case, output "YES" (without quotes) if Alperen passes the candy, and "NO" (without quotes) otherwise.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Note: In the first test case, Alperen follows the path $$$$$$(0,0) \overset{\texttt{U}}{\to} (0,1) \overset{\texttt{U}}{\to} (0,2) \overset{\texttt{U}}{\to} (0,3) \overset{\texttt{R}}{\to} (1,3) \overset{\texttt{D}}{\to} (1,2) \overset{\texttt{D}}{\to} \color{green}{\mathbf{(1,1)}} \overset{\texttt{L}}{\to} (0,1).$$$$$$ Note that Alperen doesn't need to end at the candy's location of $$$(1,1)$$$, he just needs to pass it at some point.In the second test case, Alperen follows the path $$$$$$(0,0) \overset{\texttt{U}}{\to} (0,1) \overset{\texttt{R}}{\to} \color{green}{\mathbf{(1,1)}}.$$$$$$In the third test case, Alperen follows the path $$$$$$(0,0) \overset{\texttt{R}}{\to} (1,0) \overset{\texttt{R}}{\to} (2,0) \overset{\texttt{R}}{\to} (3,0) \overset{\texttt{U}}{\to} (3,1) \overset{\texttt{U}}{\to} (3,2) \overset{\texttt{D}}{\to} (3,1) \overset{\texttt{D}}{\to} (3,0) \overset{\texttt{D}}{\to} (3,-1).$$$$$$In the fourth test case, Alperen follows the path $$$$$$(0,0) \overset{\texttt{L}}{\to} (-1,0) \overset{\texttt{L}}{\to} (-2,0) \overset{\texttt{L}}{\to} (-3,0).$$$$$$

Sample Inputs:
7
7
UUURDDL
2
UR
8
RRRUUDDD
3
LLL
4
DUUR
5
RUDLL
11
LLLLDDRUDRD

Sample Outputs:
YES
YES
NO
NO
YES
YES
NO

Solution (Python):
def Following_Directions(s):
    (U, D, R, L) = (0, 0, 0, 0)
    location = [0, 0]
    target = [1, 1]
    for i in range(len(s)):
        if s[i] == 'U':
            location[1] += 1
        elif s[i] == 'D':
            location[1] -= 1
        elif s[i] == 'R':
            location[0] += 1
        elif s[i] == 'L':
            location[0] -= 1
        if location == target:
            return 'YES'
    return 'NO'
t = int(input())
for i in range(t):
    len_s = int(input())
    s = input()
    print(Following_Directions(s))

Solution (Python):
def main():
    t: int = int(input())
    for _ in range(t):
        n: int = int(input())
        moves: str = input()
        x: int = 0
        y: int = 0
        ans: str = 'NO'
        for move in moves:
            if move == 'U':
                y += 1
            elif move == 'R':
                x += 1
            elif move == 'D':
                y -= 1
            else:
                x -= 1
            if x == 1 and y == 1:
                ans = 'YES'
                break
        print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
t = int(input())
for i in range(t):
    n = int(input())
    s = input()
    (x, y) = (0, 0)
    (candy_x, candy_y) = (1, 1)
    for move in s:
        if move == 'L':
            x -= 1
        elif move == 'R':
            x += 1
        elif move == 'U':
            y += 1
        elif move == 'D':
            y -= 1
        if x == candy_x and y == candy_y:
            print('YES')
            break
    else:
        print('NO')