Simple C++ Solution using Stack (Beats 100%)
Intuition\nThe problem asks us to compare two strings, considering the \'#\' character as a backspace. In other words, if there is a \'#\' character in a string
# Intuition\nThe problem asks us to compare two strings, considering the \'#\' character as a backspace. In other words, if there is a \'#\' character in a string, it should erase the character before it. We need to determine if two strings are equal after considering all the backspaces.\n\n# Approach\nThe provided code is a C++ solution that uses two stacks, s1 and s2, to process the input strings s and t. It iterates through the strings, pushing characters onto the stack unless a \'#\' is encountered. When a \'#\' is encountered, it pops the top character from the stack (effectively simulating the backspace).\n\nAfter processing both strings, it compares the resulting stacks to check if they are equal. If the stacks are equal in size and their elements are the same when popped from both stacks, the original strings are equal.\n\n# Complexity\n- Time complexity: O(max(n, m))\nwhere n is the length of string s, and m is the length of string t. We iterate through both strings once.\n\n- Space complexity: O(max(n, m))\nas the space used for the stacks is proportional to the length of the longer string.\n\n# Code\n```\nclass Solution {\npublic:\n    bool backspaceCompare(string s, string t) {\n        \n        stack<char> s1, s2;\n        // check elements of s\n        for(int i=0; i<s.size(); i++){\n            if(s[i]!=\'#\')\n            s1.push(s[i]);\n            else if(!s1.empty())\n            s1.pop();\n        }\n\n        // check elements of t\n        for(int i=0; i<t.size(); i++){\n            if(t[i]!=\'#\')\n            s2.push(t[i]);\n            else if(!s2.empty())\n            s2.pop();\n        }\n\n        if(s1.size()!=s2.size()) return false;\n        \n        while (!s1.empty()) {\n            if (s1.top() != s2.top()) {\n                return false;\n            }\n            s1.pop();\n            s2.pop();\n        }\n\n        return true;\n    }\n};\n```
['C++']