Given a robot which can only move in four directions, UP(U), DOWN(D), LEFT(L), RIGHT(R). Given a string consisting of instructions to move. Output the coordinates of a robot after executing the instructions. Initial position of robot is at origin(0, 0).
Input : move = "UDDLRL" Output : (-1, -1) Move U : (0, 0)--(0, 1) Move D : (0, 1)--(0, 0) Move D : (0, 0)--(0, -1) Move L : (0, -1)--(-1, -1) Move R : (-1, -1)--(0, -1) Move L : (0, -1)--(-1, -1) Therefore final position after the complete movement is: (-1, -1) Input : move = "UDDLLRUUUDUURUDDUULLDRRRR" Output : (2, 3)
Approach: Count number of up movements (U), down movements (D), left movements (L) and right movements (R) as countUp, countDown, countLeft and countRight respectively. Final x-coordinate will be
(countRight – countLeft) and y-coordinate will be (countUp – countDown).
Final Position: (2, 3)
- Check if a given sequence of moves for a robot is circular or not
- Find position of the given number among the numbers made of 4 and 7
- Reorder the position of the words in alphabetical order
- Find letter's position in Alphabet using Bit operation
- Count of pairs of strings which differ in exactly one position
- Count characters at same position as in English alphabet
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Count strings with consonants and vowels at alternate position
- Goldman Sachs Interview for SDE1 position(One year experienced).
- Arrangement of words without changing the relative position of vowel and consonants
- Find the number of occurrences of a character upto preceding position
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Remove characters from a String that appears exactly K times
- Minimum swaps to make two strings equal by swapping only with third string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.