problems / 188

Suki's Muddy Pawprints

easy matrix simulation arrays 1000 ms · 256 MB

Suki's Muddy Pawprints

Suki, a black curly-coated dog with a grey beard and white paws, is walking across a floor, leaving muddy paw prints at each step. She starts at position (0, 0) and follows a sequence of directional moves.

Given her sequence of moves, count how many unique cells she visits (including her starting position) and determine whether she ever revisits a cell.

Input

The first line contains n (1 ≤ n ≤ 100,000), the number of moves Suki makes.

The second line contains a string of n characters, where each character is one of: U (up), D (down), L (left), or R (right).

Output

Print two lines:

  1. The count of unique cells visited.
  2. Yes if Suki revisits any cell, or No if she only visits each cell at most once.

Notes

  • Suki starts at position (0, 0), which counts as one visited cell.
  • Coordinates are in the plane: up increases y, down decreases y, left decreases x, right increases x.
  • There are no bounds on the floor size; Suki can walk as far as needed.

Example 1

Input

2
RU

Output

3
No

Example 2

Input

3
RLR

Output

2
Yes

Hints · reveal one at a time if you're stuck

Hint 1

Each move reaches one new position, and there are at most 100000 moves. How would you quickly tell whether a position has been visited before?

Hint 2

Store every visited position, including the start, in a hash set. The floor has no bounds, so coordinates can be negative and you cannot use a fixed-size grid.

report an issue Prefer your own editor? sooks prep 188 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.