✅C++ solution with multiple approach, detailed explained with time complexity
If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistakes please let me know. Thank you!\u2
> **If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistakes please let me know. Thank you!\u2764\uFE0F**\n> \n\n---\n\n## Explanation:\n\n### Solution 01\n\n- Using ***Stack.***\n- Here we\u2019ll iterate both strings and will put every character in the stack. There are 3 conditions:\n    - `if(x==\'#\' && !mp1.empty())` then we\u2019ve to pop the last element from the stack.\n    - `if(x==\'#\' && mp1.empty())` Here we have no element to pop, simply skip this.\n    - `else` we\u2019ll push every other character to the stack.\n- We\u2019ve to do this operation for both the strings. And at last we\u2019ll compare both the stacks.\n- If both stacks are equal then return true, else false.\n- **Time complexity:** O(n).\n\n### Solution 02\n\n- Using ***Two pointers.***\n- Start traversing the string by the first pointer\xA0***i***. if it is pointing to\xA0**#**\xA0then decrease the second pointer ***x.***\xA0Make sure that\xA0***x***\xA0is always greater than or equals 0. And if it is not pointing to\xA0**#**\xA0then increase the second pointer\xA0***x***\xA0and store the element at\xA0***xth***\xA0position.\xA0**`s[x]=s[i]`**continue the process until the string ends.\n- We will do the same process with the second string with its two pointers\xA0***i and y***.\n- Now after traversing both the strings. If\xA0***x and y***\xA0are not equal then it means that the two strings have different lengths. So, `return false.`\n- If\xA0***x and y***\xA0are the same, then compare elements of every index. If at any point the two elements of the same index of both the string are different then `return false.`\n- Else `return true.`\n- **Time complexity:** O(n).\n\n---\n\n## Code:\n\n```cpp\n//Solution 01:\nclass Solution {\npublic:\n    bool backspaceCompare(string s, string t) {\n        stack<char> mp1, mp2;\n        for(auto x:s){\n            if(x==\'#\' && !mp1.empty()) mp1.pop();\n            else if(x==\'#\' && mp1.empty()) continue;\n            else mp1.push(x);\n        }\n        for(auto x:t){\n            if(x==\'#\' && !mp2.empty()) mp2.pop();\n            else if(x==\'#\' && mp2.empty()) continue;\n            else mp2.push(x);\n        }\n        if(mp1 == mp2) return true;\n        return false;\n    }\n};\n\n//Solution 02:\nclass Solution {\npublic:\n    bool backspaceCompare(string s, string t) {\n        int x=0, y=0;\n        \n        // for string s\n        for(int i=0; i<s.size(); i++){\n            if(s[i] == \'#\'){\n                x--;\n                x = max(0, x);\n            }\n            else{\n                s[x] = s[i];\n                x++;\n            }\n        }\n        // for string t\n        for(int i=0; i<t.size(); i++){\n            if(t[i] == \'#\'){\n                y--;\n                y = max(0, y);\n            }\n            else{\n                t[y] = t[i];\n                y++;\n            }\n        }\n        \n        if(x != y) return false;\n        else{\n            for(int i=0; i<x; i++){\n                if(s[i] != t[i]) return false;\n            }\n            return true;\n        }\n    }\n};\n```\n\n---\n\n> **Please upvote this solution**\n>
['Two Pointers', 'String', 'Stack', 'C', 'C++']