LeetCode 844. Backspace String Compare

May. 6, 2021

Here is my solution Backspace String Compare problem.

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var backspaceCompare = function(s, t) {
    s = processString(s);
    t = processString(t);
    //console.log({s, t})
    return s===t;
};

let processString = function(s) {
    const a = [];
    let skip = 0;
    for (let i = s.length-1; i >= 0; i--) {
        if (s[i] === '#') {
            skip++;
        } else if (skip > 0) {
            skip--;
        } else {
            a.push(s[i]);
        }
        //console.log({a, skip, i})
    }
    let re = '';
    //console.log({ a: a.length})
    while (a.length > 0) {
        re += a.pop();
        //console.log({i, re, a: a.length})
    }
    return re;
}

Results:

Runtime: 80 ms, faster than 73.76% of JavaScript online submissions for Backspace String Compare.

Memory Usage: 39.3 MB, less than 33.63% of JavaScript online submissions for Backspace String Compare.