VOOZH about

URL: https://www.geeksforgeeks.org/dsa/string-after-processing-backspace-characters/

⇱ String after processing backspace characters - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

String after processing backspace characters

Last Updated : 7 Sep, 2022

Given a string S containing letters and '#'. The '#" represents a backspace. The task is to print the new string without '#'.

Examples: 

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: 


Output
geefgeek

Complexity Analysis:

  • Time Complexity: O(N), where N is the length of the String.
  • Space Complexity: O(N) since using auxiliary deque
Comment