Nuggets team number online, help you Offer impromptu! Click for details

Topic describes

Given two strings S and T, when each is entered into a blank text editor, determine if the two are equal and return the result. # stands for backspace character.

Note: If you enter a backspace character for empty text, the text remains empty.

Example 1:

Enter: S ="ab#c", T = "ad#c"Output:trueExplanation: both S and T become "ac".Copy the code

Their thinking

We can use the stack to process two strings into unbackspaced strings and compare them:

  1. To start with our first string, we push each element of the string onto the stack. If we see #, we pop up an element;
  2. When we iterate through the entire string, we print out the characters on the stack as strings;
  3. Then we do the same for the second string
  4. Finally, we compare the two strings for equality

The problem solving code

var backspaceCompare = function (s, t) {
    const stackStr = [];
    for (const c of str) {
        if (c === The '#') {
            stackStr.pop();
        } else{ stackStr.push(c); }}return stackStr.join(' ');
};
Copy the code

conclusion

A journey of a thousand miles begins with a single step.