对比两个string在退格操作后是否相同

两个string 都有字母和backspace,然后看最后是不是一样的结果

从后往前traverse O(N) + O(1)

class Solution {
    public boolean areSameStrings(String s1, String s2) {
        if (s1 == null || s2 == null) {
            return false;
        }
        if (s1.equals(s2)) {
            return true;
        }

        int i = s1.length() - 1;
        int j = s2.length() - 1;
        while (i >= 0 && j >= 0) {
            while (s1.charAt(i) == '$') {
                i -= 2;
            }

            while (s2.charAt(j) == '$') {
                j -= 2;
            }

            if (s1.charAt(i) != s2.charAt(j)) {
                return false;
            }

            i--;
            j--;
        } // end of while

        if (i != -1 || j != -1) {
            return false;
        }

        return true;
    }
}

你可能感兴趣的:(对比两个string在退格操作后是否相同)