The title

Given two strings, s and t, when each is entered into a blank text editor, determine whether the two are equal. # stands for backspace character.

Return true if equal; Otherwise, return false.

Example 1:

Input: s = "ab#c", t = "ad#c" output: true explanation: both s and t become "ac".Copy the code

Source: LeetCode leetcode-cn.com/problems/ba…

Their thinking

  1. The # sign stands for fallback, that is, removing the preceding characters. This is easily handled using a stack, where the characters are pushed in sequence and the top element is removed when the # sign is encountered.
  2. Compare the two strings to see if they are equal after the first step.
  3. In the exampleab#cSo if I get rid of the b is equal to thetaac.ad#cWhen I get to # ok, I get rid of the d is equal toac, so return true

Code implementation

Var backspaceCompare = function (s, t) {return convertString(s) === convertString(t)}; Var convertString = function (s) {var convertString = function (s) { Let stack = [] for (let c of s) {if (c === '#') {stack.pop()} else {stack.push(c)}} return stack.join('')}Copy the code

If there are mistakes welcome to point out, welcome to discuss together!