Check if two strings after processing backspace character are equal or not | Java 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
import java . util . * ; import java . lang . * ; class GFG { static boolean compare ( StringBuilder s , StringBuilder t ) { int ps , pt , i ; ps = - 1 ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' # ' && ps != - 1 ) ps -= 1 ; else if ( s . charAt ( i ) != ' # ' ) { s . setCharAt ( ps + 1 , s . charAt ( i ) ) ; ps += 1 ; } } pt = - 1 ; for ( i = 0 ; i < t . length ( ) ; i ++ ) { if ( t . charAt ( i ) == ' # ' && pt != - 1 ) pt -= 1 ; else if ( t . charAt ( i ) != ' # ' ) { t . setCharAt ( pt + 1 , t . charAt ( 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 . charAt ( i ) != t . charAt ( i ) ) return false ; } return true ; } } public static void main ( String [ ] args ) { StringBuilder s = new StringBuilder ( " geee # e # ks " ) ; StringBuilder t = new StringBuilder ( " gee # # eeks " ) ; if ( compare ( s , t ) ) System . out . print ( " True " ) ; else System . out . print ( " False " ) ; } }