SIMPLE AND EASY TO UNDERSTAND JAVA CODE BY 2 APPROACHES (^///^)
APPROACH 1 :USING STACK\n\nclass Solution {\n    public boolean backspaceCompare(String s, String t) {\n        //creating 2 stacks to store character s1 for st
***APPROACH 1 :USING STACK***\n```\nclass Solution {\n    public boolean backspaceCompare(String s, String t) {\n        //creating 2 stacks to store character s1 for string s and s2 for string t\n        Stack<Character> s1 = new Stack<>();\n        Stack<Character> s2 = new Stack<>();\n        // for loop to fill the charater of s into stack s1\n        for(int i=0;i<s.length();i++){\n            char ch = s.charAt(i);\n            // if we got \'#\' while filling the stack s1 we simply remove the top value of the stack\n            if(ch==\'#\'){\n                if(!s1.isEmpty()) s1.pop();\n            }\n            else{\n                // otherwise we just keep put the charater in stack s1\n                s1.push(ch);\n            }\n            \n            \n        }\n         // for loop to fill the charater of t into stack s2\n        for(int i=0;i<t.length();i++){\n            char ch = t.charAt(i);\n             // if we got \'#\' while filling the stack s2 we simply remove the top value of the stack\n            if(ch==\'#\'){\n                if(!s2.isEmpty()) s2.pop();\n            }\n            else{\n                // otherwise we just keep put the charater in stack s2\n                s2.push(ch);\n            }\n        }\n        //Now after completely filling all the characters of s and t String we will check weather to return true or false\n        //if the size of both stack is not equal then return false\n        if(s1.size()!= s2.size()) return false;\n        //if size is equal the start a while loop which will compare the characters of both stack till s1 stack get empty\n        while(!s1.isEmpty()){\n            char ch1 = s1.pop();//Storing the top value of stack s1 in ch1\n            char ch2 = s2.pop();//Storing the top value of stack s1 in ch1\n            if(ch1!=ch2) return false;// If the top values are not same then return false else while loop runs again\n        }\n        \n        return true;// if all the conditions are satisfied then return true\n    }\n    \n}\n```\n<hr>\n<hr>\n\n***Time Complexity = O(N)***  \n***Space Complexity =O(N+M)*** where N,M are the lengths of S and T which we fill in the stack respectively.\n<hr>\n<hr>\n\n***APPROACH 2 : 2 POINTERS***\n```\npublic boolean backspaceCompare(String S, String T) {\n\tassert S != null && T != null;\n\n    // Iterate to find next pair each time\n    int i = S.length() - 1;\n    int j = T.length() - 1;\n    while (i >= 0 || j >= 0) {\n        // Skip deleted elements in both strings, until next effective character\n        i = skipBackspaceCharacters(S, i);\n        j = skipBackspaceCharacters(T, j);\n\n        // Compare character pair\n        if (i >= 0 && j >= 0) {\n            if (S.charAt(i) != T.charAt(j)) {\n                return false;\n            }\n\n            i--;\n            j--;\n        } else if ((i >= 0) != (j >= 0)) {    // Unmatched indices\n            return false;\n        }\n    }\n\n    return true;\n}\n\nprivate int skipBackspaceCharacters(String s, int index) {\n    // Simulate stack in & out sequence with counter\n    while (index >= 0 && s.charAt(index) == \'#\') {\n        int count = 1;\n        index--;\n\n        while (index >= 0 && count > 0) {\n            if (s.charAt(index) == \'#\') {\n                count++;\n            } else {\n                count--;\n            }\n            index--;\n        }\n    }\n\n    return index;\n}\n```\n<hr>\n<hr>\n\n***Time Complexity = O(N+M)*** where N,M are the lengths of S and T respectively.\n***Space Complexity =O(1)*** as we are not using any extra sapce\n<hr>\n<hr>
[]