Given a string S of length n representing n boxes adjacent to each other.
- A character R at index i represents that i-th box is being pushed towards the right.
- On the other hand, L at index i represents that i-th box is being pushed towards the left.
- And a . represents an empty space.
We start with initial configuration, at every time unit, a box being pushed toward right pushes next box toward right and same is true for a box being pushed toward left. The task is to print the final positions of all boxes when no more movements are possible. If there is a substring “R.L”, then middle box is being pushed from both directions and therefore not moved any more.
Input: S = “RR.L”
The first and second boxes are being toward right. The middle dot is being pushed from both directions, so does not move.
Input: S = “R..R…L.”
At time unit 1 :
The first box pushes second box.
Third box pushes fourth box.
Second last box pushes third last box, configuration becomes
At time unit 2 :
Second box pushes third box, string becomes
Approach: We can calculate the net force applied on every box. The forces we care about are how close a box is to a leftward R or to a rightward L i.e. the more closer we are, the stronger will be the force.
- Scanning from left to right, force decays by 1 in every iteration, and resets to N if we meet an R again.
- Similarly, from right to left direction, we can find the forces from the right (closeness to L).
- For some box result[i], if the forces are equal then the result is . as both side apply same force to it. Otherwise, our result is implied by whichever force is stronger.
For string S = “R..R…L.”
The forces going from left to right are [7, 6, 5, 7, 6, 5, 4, 0, 0].
The forces going from right to left are [0, 0, 0, 0, -4, -5, -6, -7, 0].
Combining them (taking their arithmetic addition at each index) gives result = [7, 6, 5, 7, 2, 0, -2, -7, 0].
Thus, the final answer is RRRRR.LL.
Below is the implementation of above approach:
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 string after performing given operations
- Print the final string when minimum value strings get concatenated in every operation
- Distinct state codes that appear in a string as contiguous sub-strings
- Find the final X and Y when they are Altering under given condition
- Final cell position in the matrix
- Minimum possible final health of the last monster in a game
- Find the final radiations of each Radiated Stations
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Find the final sequence of the array after performing given operations
- Find the final co-ordinates reached by following a sequence of directions
- Length of race track based on the final distance between participants
- Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Minimum time to return array to its original state after given modifications
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Minimal moves to form a string by adding characters or appending string itself
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Transform string str1 into str2 by taking characters from string str3
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Lexicographically smallest string formed by appending a character from the first K characters of a given 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 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.