

- 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
Program to find final text in an editor by performing typing and backspacing in Python
Suppose we have a string s that represents characters that typed into an editor, the symbol "<-" indicates a backspace, we have to find the current state of the editor.
So, if the input is like s = "ilovepython<-<-ON", then the output will be "ilovepythON", as there are two backspace character after "ilovepython" it will delete last two character, then again type "ON".
To solve this, we will follow these steps −
- res := a new list
- for each i in s, do
- if i is same as '-' and last character of res is same as '<', then
- delete last element from res
- if res is not empty, then
- delete last element from res
- otherwise,
- insert i at the end of res
- if i is same as '-' and last character of res is same as '<', then
- join the elements present in res and return
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, s): res = [] for i in s: if i == '-' and res[-1] == '< ': res.pop() if res: res.pop() else: res.append(i) return "".join(res) ob = Solution() print(ob.solve("ilovepython<-<-ON"))
Input
"ilovepython<-<-ON"
Output
ilovepython<-<-ON
- Related Questions & Answers
- Program to check final answer by performing given stack operations in Python
- Program to find maximum sum by performing at most k negate operations in Python
- How to Use Nano Text Editor
- Program to make all elements equal by performing given operation in Python
- Final string after performing given operations in C++
- Program to find maximum score from performing multiplication operations in Python
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- How to add a text editor field in Django?
- Program to find final states of rockets after collision in python
- Creating a Rich Text Editor in React JS
- How to Install Sublime Text Editor on Ubuntu
- Python Tkinter – How to display a table editor in a text widget?
- Performing text data analysis and Search capability in SAP HANA
- C++ program to count final number of characters are there after typing n characters in a crazy writer
- How to choose an HTML Editor?
Advertisements