Back to all solutions

#844 - Backspace String Compare

Problem Description

Given two strings `s` and `t`, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.

Note that after backspacing an empty text, the text will continue empty.

Solution

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var backspaceCompare = function(s, t) {
  return handleBackspaces(s) === handleBackspaces(t);
};

function handleBackspaces(input) {
  return input.split('').reduce((result, char) => {
    if (char === '#') {
      result.pop();
    } else {
      result.push(char);
    }
    return result;
  }, []).join('');
}