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##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 = equ#ual, after processing the backspace character becomes “equal” whereas string 2 = equ#ual, after processing the backspace character becomes “equaa”. 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:
C++
/* C++ 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 string removeBackspaces(string& s) { int n = s.size(); // To point at position after considering the // backspaces int idx = 0; for ( int i = 0; i < n; i++) { if (s[i] != '#' ) { s[idx] = s[i]; idx++; } else if (s[i] == '#' && idx >= 0) { idx--; } // This idx can never point at negative index // position if (idx < 0) idx = 0; } return s.substr(0, idx); } // Driver code int main() { // initialise two strings string s = "equ#ual" ; string t = "gee##eeks" ; if (removeBackspaces(s) == removeBackspaces(t)) cout << "True" ; else cout << "False" ; return 0; } |
Java
/* Java implementation to Check if two strings after processing backspace character are equal or not*/ import java.io.*; public class Main { // function to compare the two strings public static String removeBackspaces(String s) { int n = s.length(); // To point at position after considering the // backspaces int idx = 0 ; for ( int i = 0 ; i < n; i++) { if (s.charAt(i) != '#' ) { s = s.substring( 0 , idx) + s.charAt(i) + s.substring(idx + 1 ); idx++; } else if (s.charAt(i) == '#' && idx >= 0 ) { idx--; } // This idx can never point at negative index // position if (idx < 0 ) idx = 0 ; } return s.substring( 0 , idx); } // Driver code public static void main(String[] args) { // initialise two strings String s = "equ#ual" ; String t = "gee##eeks" ; if (removeBackspaces(s).equals(removeBackspaces(t))) System.out.println( "True" ); else System.out.println( "False" ); } } // This code is contributed by ritaagarwal. |
Python3
# Python implementation to Check if two strings after processing backspace character are equal or not # function to compare the two strings def removeBackspace(s) - > str : n = len (s) # To point at position after considering the backspaces idx = 0 for i in range ( 0 , n): if (s[i] ! = '#' ): s = s[:idx] + s[i] + s[idx + 1 :] idx + = 1 elif (s[i] = = '#' and idx > = 0 ): idx - = 1 # This idx can never point at negative index position if (idx < 0 ): idx = 0 ans = "" for i in range ( 0 , idx): ans + = s[i] return ans # Driver code s = "equ#ual" t = "gee##eeks" if (removeBackspace(s) = = removeBackspace(t)): print ( "TRUE" ) else : print ( "FALSE" ) |
C#
// C# implementation of above approach using System; class GFG { // function to compare the two strings static string removeBackspaces( string s) { int n = s.Length; char [] ch = s.ToCharArray(); // To point at position after considering the // backspaces int idx = 0; for ( int i = 0; i < n; i++) { if (s[i] != '#' ) { ch[idx] = s[i]; idx++; } else if (s[i] == '#' && idx >= 0) { idx--; } // This idx can never point at negative index // position if (idx < 0) idx = 0; } s = new string (ch); return s.Substring(0, idx); } // Driver code public static void Main() { // initialise two strings string s = "equ#ual" ; string t = "gee##eeks" ; if (removeBackspaces(s) == removeBackspaces(t)) Console.Write( "True" ); else Console.Write( "False" ); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
/* Javascript implementation to Check if two strings after processing backspace character are equal or not*/ // function to compare the two strings function removeBackspaces(s) { let n = s.length; // To point at position after considering the // backspaces let idx = 0; for (let i = 0; i < n; i++) { if (s[i] != '#' ) { s[idx] = s[i]; idx++; } else if (s[i] == '#' && idx >= 0) { idx--; } // This idx can never point at negative index // position if (idx < 0) idx = 0; } return s.substring(0, idx); } // Driver code // initialise two strings let s = "equ#ual" ; let t = "gee##eeks" ; if (removeBackspaces(s) == removeBackspaces(t)) console.log( "True" ); else console.log( "False" ); // This code is contributed by poojaagarwal2. |
False
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach: (Using Stack)
The idea is to use a stack that stores the last occurrence of a character other than “#” (i.e, Backspace) and iterate over the each given string and if we find any character other than ‘#’ then we keep storing it into the stack otherwise remove the last occurred character which is store into the stack. Do the Similar process for both given strings and check if both strings are equal then print “Yes” otherwise “No”.
Following is the implementation of the above approach:
C++
// C++ implementation to Check if // two strings after processing // backspace character are equal or not #include <iostream> #include <string> using namespace std; // function to remove backspaces and return refined string string remove_backspace(string str) { string res; for ( char c : str) { if (c != '#' ) { res.push_back(c); } else if (!res.empty()) { res.pop_back(); } } return res; } // function to compare the two strings bool compare(string s, string t) { s = remove_backspace(s); t = remove_backspace(t); return s == t; } // Driver code int main() { string s = "geee#e#ks" ; string t = "gee##eeks" ; if (compare(s, t)) { cout << "True" << endl; } else { cout << "False" << endl; } return 0; } // This code is contributed by princekumaras |
Java
// Java implementation to Check if // two strings after processing // backspace character are equal or not import java.util.*; public class BackspaceStringCompare { // function to remove backspaces and return refined string static String remove_backspace(String str) { StringBuilder res = new StringBuilder(); for ( char c : str.toCharArray()) { if (c != '#' ) { res.append(c); } else if (res.length() > 0 ) { res.deleteCharAt(res.length() - 1 ); } } return res.toString(); } // function to compare the two strings static boolean compare(String s, String t) { s = remove_backspace(s); t = remove_backspace(t); return s.equals(t); } // Driver code public static void main(String[] args) { String s = "geee#e#ks" ; String t = "gee##eeks" ; if (compare(s, t)) { System.out.println( "True" ); } else { System.out.println( "False" ); } } } // This code is contributed by adityashatmfh |
Python3
# Python implementation to Check if # two strings after processing # backspace character are equal or not # function to compare the two strings def compare(s, t): # function to remove backspaces and return refined string def remove_backspace(string): a = [] for i in string: if i ! = "#" : a.append(i) else : if len (a): a.pop() return "".join(a) s, t = remove_backspace(s), remove_backspace(t) #remove backspaces from the strings return s = = t #return True if they are equal # Driver code # initialise two strings s = "geee#e#ks" t = "gee##eeks" if (compare(s, t)): print ( "True" ) else : print ( "False" ) # This code is Contributed by Vivek Maddeshiya |
C#
// C# implementation to Check if // two strings after processing // backspace character are equal or not using System; using System.Text; public class BackspaceStringCompare { // function to remove backspaces and return refined string static string RemoveBackspace( string str) { StringBuilder res = new StringBuilder(); foreach ( char c in str) { if (c != '#' ) { res.Append(c); } else if (res.Length > 0) { res.Remove(res.Length - 1, 1); } } return res.ToString(); } // function to compare the two strings static bool Compare( string s, string t) { s = RemoveBackspace(s); t = RemoveBackspace(t); return s.Equals(t); } // Driver code public static void Main( string [] args) { string s = "geee#e#ks" ; string t = "gee##eeks" ; if (Compare(s, t)) { Console.WriteLine( "True" ); } else { Console.WriteLine( "False" ); } } } // This code is contributed by codebraxnzt |
Javascript
// JavaScript program to check if two strings after processing // backspace character are equal or not // function to compare two strings function compare(s, t){ // function to remove backspaces and return refined string function remove_backspaces(string){ a = []; for (let i = 0; i<string.length; i++){ if (string[i] != '#' ) a.push(string[i]); else if (a.length > 0) a.pop(); } return a.join( "" ) } s = remove_backspaces(s); t = remove_backspaces(t); return s == t; } // driver program for above functions let s = "geee#e#ks" ; let t = "gee##eeks" ; if (compare(s,t)) console.log( "True" ); else console.log( "False" ); // THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGARWAL2852002) |
True
Time Complexity: O(n), Where n is the length of the given string
Auxiliary Space: O(n)
Please Login to comment...