Approach
Since we just need to know if it will return to it’s starting place, just check if all moves cancel out (equal rights to lefts and up’s to downs.)
Search
Jun 27, 20241 min readThe time complexity of the given solution is O(n), where n is the length of the input string `moves`. This is because the `count` method is called four times, each of which iterates through the entire string to count the occurrences of 'U', 'D', 'L', and 'R'. Therefore, the overall time complexity is linear with respect to the size of the input. The space complexity is O(1), as the solution uses a constant amount of space regardless of the input size. The only additional space used is for storing the counts of the moves, which does not depend on the length of the input string. Thus, the space complexity remains constant.
Since we just need to know if it will return to it’s starting place, just check if all moves cancel out (equal rights to lefts and up’s to downs.)