GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #844 - Backspace String Compare - LeetCode JavaScript Solutions

Back to all solutions

#844 - Backspace String Compare

Easy • View on LeetCode • View on GitHub
String Two Pointers Simulation Stack

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('');
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.