String after processing backspace characters Last Updated : 07 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: C++ // CPP implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to find new final String string newString(string S) { deque<char> q; for (int i = 0; i < S.length(); ++i) { if (S[i] != '#') q.push_back(S[i]); else if (!q.empty()) q.pop_back(); } // build final string string ans = ""; while (!q.empty()) { ans += q.front(); q.pop_front(); } // return final string return ans; } // Driver program int main() { string S = "##geeks##for##geeks#"; // function call to print required answer cout << newString(S); return 0; } // This code is contributed by Sanjit_Prasad Java // Java implementation of above approach import java.util.*; class GFG { // Function to find new final String static String newString(String S) { Stack<Character> q = new Stack<Character>(); for (int i = 0; i < S.length(); ++i) { if (S.charAt(i) != '#') q.push(S.charAt(i)); else if (!q.isEmpty()) q.pop(); } // build final string String ans = ""; while (!q.isEmpty()) { ans += q.pop(); } // return final string String answer = ""; for(int j = ans.length() - 1; j >= 0; j--) { answer += ans.charAt(j); } return answer; } // Driver Code public static void main(String[] args) { String S = "##geeks##for##geeks#"; // function call to print // required answer System.out.println(newString(S)); } } // This code is contributed // by prerna saini Python3 # Python3 implementation of above approach # Function to find new final String def newString(S): q = [] for i in range(0, len(S)): if S[i] != '#': q.append(S[i]) elif len(q) != 0: q.pop() # Build final string ans = "" while len(q) != 0: ans += q[0] q.pop(0) # return final string return ans # Driver Code if __name__ == "__main__": S = "##geeks##for##geeks#" # Function call to print # required answer print(newString(S)) # This code is contributed by Rituraj Jain C# // C# implementation of above approach using System.Collections.Generic; using System; class GFG { // Function to find new final String static String newString(String S) { Stack<Char> q = new Stack<Char>(); for (int i = 0; i < S.Length; ++i) { if (S[i] != '#') q.Push(S[i]); else if (q.Count!=0) q.Pop(); } // build final string String ans = ""; while (q.Count!=0) { ans += q.Pop(); } // return final string String answer = ""; for(int j = ans.Length - 1; j >= 0; j--) { answer += ans[j]; } return answer; } // Driver Code public static void Main(String []args) { String S = "##geeks##for##geeks#"; // function call to print // required answer Console.WriteLine(newString(S)); } } // This code is contributed by 29AjayKumar JavaScript <script> // Javascript implementation of above approach // Function to find new final String function newString(S) { let q = []; for (let i = 0; i < S.length; ++i) { if (S[i] != '#') q.push(S[i]); else if (q.length!=0) q.pop(); } // build final string let ans = ""; while (q.length!=0) { ans += q.pop(); } // return final string let answer = ""; for(let j = ans.length - 1; j >= 0; j--) { answer += ans[j]; } return answer; } // Driver Code let S = "##geeks##for##geeks#"; // function call to print // required answer document.write(newString(S)+"<br>"); // This code is contributed by rag2127 </script> Outputgeefgeek Complexity Analysis: Time Complexity: O(N), where N is the length of the String.Space Complexity: O(N) since using auxiliary deque Comment More infoAdvertise with us Next Article Reverse a string without affecting special characters S Sanjit_Prasad Follow Improve Article Tags : Strings Queue DSA cpp-deque deque cpp-strings +2 More Practice Tags : cpp-stringsDequeQueueStrings Similar Reads Check if two strings after processing backspace character are equal or not Given two strings s1 and s2, let us assume that while typing the strings there were some backspaces encountered which are represented by #. The task is to determine whether the resultant strings after processing the backspace character would be equal or not. Examples: Input: s1= geee#e#ks, s2 = gee# 8 min read How does Java process the backspace terminal control character? What is the backspace terminal control character? In this article, we will discuss about the backspace terminal control character. It is used to move the cursor one character back. For backspace terminal control we use '\b' notation in Java. What does the backspace terminal control character do? By 2 min read Final string after performing given operations Given a string str containing only characters x and y, the task is to perform the following operations while possible: Find an index such that s[i] = 'x' and s[i+1] = 'y' and delete both the characters s[i] and s[i+1], if no such index is found then find an index such that s[i] = 'y' and s[i+1] = 'x 6 min read Reverse a string without affecting special characters Given a string, that contains a special character together with alphabets ('a' to 'z' and 'A' to 'Z'), reverse the string in a way that special characters are not affected. Examples: Input: str = "a,b$c"Output: str = "c,b$a"Explanation: Note that $ and , are not moved anywhere. Only subsequence "abc 10 min read Remove all characters other than alphabets from string Given a string consisting of alphabets and others characters, remove all the characters other than alphabets and print the string so formed. Examples: Input : $Gee*k;s..fo, r'Ge^eks?Output : GeeksforGeeks Input : P&ra+$BHa;;t*ku, ma$r@@s#in}ghOutput : PraBHatkumarsingh Recommended PracticeRemove 12 min read Program for removing i-th character from a string Given a string S along with an integer i. Then your task is to remove ith character from S. Examples: Input: S = Hello World!, i = 7Output: Hello orld!Explanation: The Xth character is W and after removing it S becomes Hello orld! Input: S = GFG, i = 1Output: GGExplanation: It can be verified that a 5 min read Like