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##eeks Output: True Explanation: Both the strings after processing the backspace character becomes "geeeeks". Hence, true. Input: s1 = equ#ual, s2 = ee#quaal# Output: False Explanation: String 1 after processing the backspace character becomes "equal" whereas string 2 is "eequaal". Hence, false.
Approach:
To solve the problem mentioned above we have to observe that if the first character is ‘#’, that is there is certainly no character typed initially and hence we perform no operation. When we encounter any character other than ‘#’, then we add the character just after the current index. When we encounter a ‘#’, we move one index back, so instead of deleting the character, we just ignore it. Then finally compare the two strings by comparing each character from start to end.
Below is the implementation of the above approach:
/* CPP implementation to Check if two strings after processing backspace character are equal or not*/ #include <bits/stdc++.h> using namespace std; // function to compare the two strings bool compare(string s, string t) { int ps, pt, i; /* the index of character in string which would be removed when backspace is encountered*/ ps = -1; for (i = 0; i < s.size(); i++) { /* checks if a backspace is encountered or not. In case the first character is #, no change in string takes place*/ if (s[i] == '#' && ps != -1) ps -= 1; /* the character after the # is added after the character at position rem_ind1 */ else if (s[i] != '#') { s = s[i]; ps += 1; } } pt = -1; for (i = 0; i < t.size(); i++) { /* checks if a backspace is encountered or not */ if (t[i] == '#' && pt != -1) pt -= 1; else if (t[i] != '#') { t[pt + 1] = t[i]; pt += 1; } } /* check if the value of rem_ind1 and rem_ind2 is same, if not then it means they have different length */ if (pt != ps) return false; /* check if resultant strings are empty */ else if (ps == -1 && pt == -1) return true; /* check if each character in the resultant string is same */ else { for (i = 0; i <= pt; i++) { if (s[i] != t[i]) return false; } return true; } } // Driver code int main() { // initialise two strings string s = "geee#e#ks"; string t = "gee##eeks"; if (compare(s, t)) cout << "True"; else cout << "False"; } |
True
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
Recommended Posts:
- Check if two strings can be made equal by swapping one character among each other
- String after processing backspace characters
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Number of character corrections in the given strings to make them equal
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Check if there is any common character in two given strings
- Check if frequency of each character is equal to its position in English Alphabet
- Check whether two strings can be made equal by increasing prefixes
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Processing strings using std::istringstream
- Find the numbers of strings that can be formed after processing Q queries
- Count of strings that become equal to one of the two strings after one removal
- Meta Strings (Check if two strings can become same after a swap in one string)
- Sub-strings of length K containing same character
- Character pairs from two strings with even sum
- Sub-strings that start and end with one character and have at least one other
- Count of sub-strings that contain character X at least once
- Count of sub-strings that do not consist of the given character
- Number of sub-strings that contain the given character exactly k times
- Print the most occurring character in an array of strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

