[Java/C++] Efficient and Simple Solution WITHOUT Stack etc
Scan the String from right to left. Maintain the "#" count. Increment \'count\' variable if the character encountered is \'#\'. Otherwise decrement the count an
Scan the String from right to left. Maintain the "#" count. Increment \'count\' variable if the character encountered is \'#\'. Otherwise decrement the count and if the count is 0, append that character to the result.\nPerform above for both the strings, compare the results and return true/false accordingly.\n\nJAVA: \n\n```\nclass Solution {\n    \n    private String getString(String str) {\n        int n=str.length(), count=0;\n        String result="";\n        for(int i=n-1; i>=0; i--) {\n            char ch=str.charAt(i);\n            if(ch==\'#\') \n                count++;\n            else {\n                if(count>0)\n                    count--;\n                else {\n                    result+=ch;\n                }                     \n            }\n        }\n        return result;\n    }\n    \n    public boolean backspaceCompare(String S, String T) {\n        return getString(S).equals(getString(T));\n    }\n}\n```\n\nC++\n```\nclass Solution {\npublic:    \n    string getString(string &str) {\n        int n=str.length(), count=0;\n        string result="";\n        for(int i=n-1; i>=0; i--) {\n            char ch=str[i];\n            if(ch==\'#\') \n                count++;\n            else {\n                if(count>0)\n                    count--;\n                else {\n                    result+=ch;\n                }                     \n            }\n        }\n        return result;        \n    }\n    \n    bool backspaceCompare(string S, string T) {\n        return getString(S)==getString(T);\n    }\n};\n```
[]