Given a string S containing letters and ‘#‘. The ‘#” represents a backspace. The task is to print the new string without ‘#‘.
Input : S = "abc#de#f#ghi#jklmn#op#" Output : abdghjklmo Input : S = "##geeks##for##geeks#" Output : geefgeek
Approach: A simple approach to this problem by using deque is as follows:
Traverse the string S. If any character except ‘#’ is found push it at back in deque. if the character ‘#’ is found pop a character from back of deque. Finally pop all elements from front of deque to make new string.
Below is the implementation of above approach:
# Python3 implementation of above approach
# Function to find new final String
q = 
for i in range(0, len(S)):
if S[i] != ‘#’:
elif len(q) != 0:
# Build final string
ans = “”
while len(q) != 0:
ans += q
# return final string
# Driver Code
if __name__ == “__main__”:
S = “##geeks##for##geeks#”
# Function call to print
# required answer
# This code is contributed by Rituraj Jain
Time Complexity: O(N), where N is the length of the String.
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Minimal moves to form a string by adding characters or appending string itself
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Find the smallest window in a string containing all characters of another string
- Check whether second string can be formed from characters of first string
- Remove characters from the first string which are present in the second string
- String matching where one string contains wildcard characters
- Mirror characters of a string
- Sort string of characters
- Roll the characters of string
- Rearrange characters in a string such that no two adjacent are same
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.