

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Final state of the string after modification in Python
<p>Suppose we have a string S. The length is n. These n boxes adjacent to each other, a character R at position i represents that i-th box is being pushed towards right. similarly, L at position i represents that i-th box is being pushed towards left, a dot '.' indicates an empty space. Starting from initial configuration, at every time unit, a box being pushed to the right side is able to push next box to right, same action can be applied for the left side also. We have to find the final positions of all boxes when no more movements are feasible.</p><p>So, if the input is like R..R...L., then the output will be RRRRR.LL.</p><p>To solve this, we will follow these steps −</p><ul class="list"><li>N := size of string</li><li>movement := array of size N, fill with 0s</li><li>m := 0</li><li>for i in range 0 to N, do<ul class="list"><li>if string[i] is same as 'R', then<ul class="list"><li>m := N</li></ul></li><li>otherwise when string[i] is same as 'L', then<ul class="list"><li>m := 0</li></ul></li><li>otherwise,<ul class="list"><li>m := maximum of m - 1, 0</li></ul></li><li>movement[i] := movement[i] + m</li></ul></li><li>m := 0</li><li>for i in range N - 1 to -1, decrease by 1, do<ul class="list"><li>if string[i] is same as 'L', then<ul class="list"><li>m := N</li></ul></li><li>otherwise when string[i] is same as 'R', then<ul class="list"><li>m := 0</li></ul></li><li>otherwise,<ul class="list"><li>m := maximum of m - 1, 0</li></ul></li><li>movement[i] := movement[i] - m</li></ul></li><li>return make a string by adding dot if m is 0 otherwise 'R' when m > 0, otherwise 'L' for every element m in movement.</li></ul><h2 style="">Example Code</h2><p>Let us see the following implementation to get better understanding −</p><p><a class="demo" href="http://tpcg.io/QDCIEmSf" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">def get_final_pos(string): N = len(string) movement = [0] * N m = 0 for i in range(0, N): if string[i] == 'R': m = N elif string[i] == 'L': m = 0 else: m = max(m - 1, 0) movement[i] += m m = 0 for i in range(N - 1, -1, -1): if string[i] == 'L': m = N elif string[i] == 'R': m = 0 else: m = max(m - 1, 0) movement[i] -= m return "".join('.' if m == 0 else 'R' if m > 0 else 'L' for m in movement) print(get_final_pos('R..R...L.'))</pre><h2>Input</h2><pre class="result notranslate">'R..R...L.'</pre><h2>Output</h2><pre class="result notranslate">RRRRR.LL.</pre>
- Related Questions & Answers
- Final string after performing given operations in C++
- Program to get final string after shifting characters with given number of positions in Python
- Program to find final states of rockets after collision in python
- Program to find state of prison cells after k days in python
- Finding state after all collisions in JavaScript
- How are entries rectified after preparing the final balance?
- Program to find next state of next cell matrix state in Python?
- How to prevent modification of object in JavaScript ?.
- State Variables via state() in Perl
- How to get creation and modification date/time of a file using Python?
- How to set creation and modification date/time of a file using Python?
- How to get file creation & modification date/times in Python?
- Program to find minimum length of string after deleting similar ends in Python
- C++ code to find final number after min max removal game
- Program to find maximum binary string after change in python
Advertisements