Given a sequence of moves for a robot, check if the sequence is circular or not. A sequence of moves is circular if first and last positions of robot are same. A move can be on of the following.
G - Go one unit L - Turn left R - Turn right
Input: path = "GLGLGLG" Output: Given sequence of moves is circular Input: path = "GLLG" Output: Given sequence of moves is circular
We strongly recommend that you click here and practice it, before moving on to the solution.
The idea is to consider the starting position as (0, 0) and direction as East (We can pick any values for these). If after the given sequence of moves, we come back to (0, 0), then given sequence is circular, otherwise not.
N | | W -------------- E | | S
The move ‘G’ changes either x or y according to following rules.
a) If current direction is North, then ‘G’ increments y and doesn’t change x.
b) If current direction is East, then ‘G’ increments x and doesn’t change y.
c) If current direction is South, then ‘G’ decrements y and doesn’t change x.
d) If current direction is West, then ‘G’ decrements x and doesn’t change y.
The moves ‘L’ and ‘R’, do not change x and y coordinates, they only change direction according to following rule.
a) If current direction is North, then ‘L’ changes direction to West and ‘R’ changes to East
b) If current direction is East, then ‘L’ changes direction to North and ‘R’ changes to South
c) If current direction is South, then ‘L’ changes direction to East and ‘R’ changes to West
d) If current direction is West, then ‘L’ changes direction to South and ‘R’ changes to North.
Below is the implementation of above idea :
Given sequence of moves is circular
Time Complexity: O(n) where n is number of moves in given sequence.
This article is contributed Kaustubh Deshmukh. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if all rows of a matrix are circular rotations of each other
- Position of robot after given movements
- Puzzle | Two robot with parachute in a line
- Check if there exists any sub-sequence in a string which is not palindrome
- Check whether frequency of characters in a string makes Fibonacci Sequence
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Convert an unbalanced bracket sequence to a balanced sequence
- Minimum moves to reach from i to j in a cyclic string
- Find the minimum number of preprocess moves required to make two strings equal
- Minimal moves to form a string by adding characters or appending string itself
- Sorted insert for circular linked list
- Minimum bit changes in Binary Circular array to reach a index
- Find the first circular tour that visits all petrol pumps
- Print all distinct circular strings of length M in lexicographical order
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Look-and-Say Sequence
- De Bruijn sequence | Set 1
- Sequence Alignment problem
- Generate a sequence with the given operations