- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
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.
So, if the input is like R..R...L., then the output will be RRRRR.LL.
To solve this, we will follow these steps −
- N := size of string
- movement := array of size N, fill with 0s
- m := 0
- for i in range 0 to N, do
- if string[i] is same as 'R', then
- m := N
- otherwise when string[i] is same as 'L', then
- m := 0
- otherwise,
- m := maximum of m - 1, 0
- movement[i] := movement[i] + m
- if string[i] is same as 'R', then
- m := 0
- for i in range N - 1 to -1, decrease by 1, do
- if string[i] is same as 'L', then
- m := N
- otherwise when string[i] is same as 'R', then
- m := 0
- otherwise,
- m := maximum of m - 1, 0
- movement[i] := movement[i] - m
- if string[i] is same as 'L', then
- return make a string by adding dot if m is 0 otherwise 'R' when m > 0, otherwise 'L' for every element m in movement.
Example Code
Let us see the following implementation to get better understanding −
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.'))
Input
'R..R...L.'
Output
RRRRR.LL.
- Related Articles
- 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
- How are entries rectified after preparing the final balance?
- Venues of Health Habit Modification
- How to get file creation & modification date/times in Python?
- How to prevent modification of object in JavaScript ?.
- Program to find maximum binary string after change in python
- Program to find minimum length of string after deleting similar ends in Python
- Finding state after all collisions in JavaScript
- 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?
- Why String class is immutable or final in Java?
- Program to find string after removing consecutive duplicate characters in Python

Advertisements