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:
Time Complexity: O(N), where N is the length of the String.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if two strings after processing backspace character are equal or not
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Find the numbers of strings that can be formed after processing Q queries
- Maximum non-repeating characters after removing K characters
- Maximum length of balanced string after swapping and removal of characters
- Check if string remains palindrome after removing given number of characters
- Lexicographically largest possible String after removal of K characters
- Find value after N operations to remove N characters of string S with given constraints
- Largest string obtained in Dictionary order after deleting K characters
- Minimum sum of squares of character counts in a given string after removing k characters
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Processing strings using std::istringstream
- Maximum length substring having all same characters after k changes
- Count of characters in str1 such that after deleting anyone of them str1 becomes str2
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Replace minimal number of characters to make all characters pair wise distinct
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.