This is a problem that involves just simulating what’s stated in the problem. There’s no hidden trick, or optimization - simply simulate a point moving from to the directions given in the string. We use a set to store all visited points starting with . If we come across any point that’s already visited, we return True, else False.
Code:
Python
Big O Analysis
-
Runtime
The runtime complexity here is as since we would be visiting all characters in the string at least once.
-
Memory
The memory usage is since we are using a set to store visited points.
— A