[Java/Python 3] Time O(max(n, m)) space O(1) clean codes
Method 1:\nTime & space: O(max(n, m)), where n = S.length(), m = T.length().\n\njava\n    public boolean backspaceCompare(String S, String T) {\n        return 
**Method 1:**\nTime & space: O(max(n, m)), where n = S.length(), m = T.length().\n\n```java\n    public boolean backspaceCompare(String S, String T) {\n        return trans(S).equals(trans(T));\n    }\n    private String trans(String str) {\n        StringBuilder sb = new StringBuilder();\n        for (char c : str.toCharArray()) {\n            if (c != \'#\') { sb.append(c); } // if not \'#\', append it at the end of sb.\n            else if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } // remove last char in sb, if sb is not empty.\n        }\n        return sb.toString();\n    }\n```\n\n----\n\n**Method 2:**\nTime: O(max(n, m)), space: O(1), where n = S.length(), m = T.length().\n\n```java\n    public boolean backspaceCompare(String S, String T) {\n        int i = S.length() - 1, j = T.length() - 1;\n        while (i >= 0 || j >= 0) {\n            i = next(S, i);\n            j = next(T, j);\n            if (i >= 0 && j >= 0 && S.charAt(i) == T.charAt(j)) {\n                --i;\n                --j;\n            }else {\n                return i < 0 && j < 0;\n            }\n        }\n        return true;\n    }\n    private int next(String S, int i) {\n       for (int backCnt = 0; i >= 0 && (S.charAt(i) == \'#\' || backCnt > 0); --i) {\n            backCnt += S.charAt(i) == \'#\' ? 1 : -1;\n        }\n        return i;\n    }\n```\n```python\n    def backspaceCompare(self, S: str, T: str) -> bool:\n        \n        def next(i: int, S: str) -> int:\n            backCnt = 0\n            while i >= 0 and (S[i] == \'#\' or backCnt > 0):\n                backCnt += 1 if S[i] == \'#\' else -1\n                i -= 1\n            return i\n        \n        i, j = len(S) - 1, len(T) - 1\n        while i >= 0 or j >= 0:\n            i, j = next(i, S), next(j, T)\n            if i >= 0 and j >= 0 and S[i] == T[j]:\n                i -= 1\n                j -= 1\n            else:\n                return i == -1 == j\n        return True\n```
[]