
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- 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
- Program to make all elements equal by performing given operation in Python
- Program to find maximum score from performing multiplication operations in Python
- Python Tkinter – How to display a table editor in a text widget?
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Final string after performing given operations in C++
- What is the best text editor to use with Python?
- Program to find final states of rockets after collision in python
- How to Use Nano Text Editor
- C++ program to count final number of characters are there after typing n characters in a crazy writer
- How to add a text editor field in Django?
- Creating a Rich Text Editor in React JS
- Program to find Final Prices With a Special Discount in a Shop in Python
- Program to Find Out the Maximum Final Power of a List in Python

Advertisements