Check if two strings after processing backspace character are equal or not | C ++ implementation to Check if two strings after processing backspace character are equal or not ; function to compare the two strings ; the index of character in string which would be removed when backspace is encountered ; checks if a backspace is encountered or not . In case the first character is # , no change in string takes place ; the character after the # is added after the character at position rem_ind1 ; checks if a backspace is encountered or not ; check if the value of rem_ind1 and rem_ind2 is same , if not then it means they have different length ; check if resultant strings are empty ; check if each character in the resultant string is same ; Driver code ; initialise two strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compare ( string s , string t ) { int ps , pt , i ; ps = -1 ; for ( i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == ' # ' && ps != -1 ) ps -= 1 ; else if ( s [ i ] != ' # ' ) { s = s [ i ] ; ps += 1 ; } } pt = -1 ; for ( i = 0 ; i < t . size ( ) ; i ++ ) { if ( t [ i ] == ' # ' && pt != -1 ) pt -= 1 ; else if ( t [ i ] != ' # ' ) { t [ pt + 1 ] = t [ i ] ; pt += 1 ; } } if ( pt != ps ) return false ; else if ( ps == -1 && pt == -1 ) return true ; else { for ( i = 0 ; i <= pt ; i ++ ) { if ( s [ i ] != t [ i ] ) return false ; } return true ; } } int main ( ) { string s = " geee # e # ks " ; string t = " gee # # eeks " ; if ( compare ( s , t ) ) cout << " True " ; else cout << " False " ; }