Given an array of list of commands U(Up), D(Down), L(Left) and R(Right) and initial cell position (x, y) in a matrix. Find the final cell position of the object in the matrix after following the given commands. It is assumed that the final required cell position exists in the matrix.
Input : command = "DDLRULL" x = 3, y = 4 Output : (1, 5) Input : command = "LLRUUUDRRDDDULRLLUDUUR" x = 6, y = 5 Output : (6, 3)
Approach: Following are the steps:
- Count cup, cdown, cleft and cright for U(Up), D(Down), L(Left) and R(Right) movements respectively.
- Calculate final_x = x + (cright – cleft) and final_y = y + (cdown – cup).
The final cell position is (final_x, final_y)
Final Position: (1, 5)
Time Complexity: O(n), where is the number of commands.
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.
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Final Matrix after incrementing submatrices by K in range given by Q queries
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Distance of nearest cell having 1 in a binary matrix
- Find minimum moves to bring all elements in one cell of a matrix
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Sum of cost of all paths to reach a given cell in a Matrix
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Maximize path sum from top-left cell to all other cells of a given Matrix
- Maximum XOR of a path from top-left to bottom-right cell of given Matrix
- Find final value if we double after every successful search in array
- Final state of the string after modification
- Find the final X and Y when they are Altering under given condition
- Find the final radiations of each Radiated Stations
- Minimum possible final health of the last monster in a game
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.