Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

844. 比较含退格的字符串 #29

Open
GpingFeng opened this issue Dec 26, 2020 · 0 comments
Open

844. 比较含退格的字符串 #29

GpingFeng opened this issue Dec 26, 2020 · 0 comments

Comments

@GpingFeng
Copy link
Owner

844. 比较含退格的字符串

解题思路

维护两个栈,最后进行对比

var backspaceCompare = function(S, T) {
    let s = [], t = [];
    let len = S.length > T.length ? S.length : T.length;
    for (let i = 0; i < len; i++) {
      if (S[i] === '#') {
        s.pop();
      } else if (S[i]) {
        s.push(S[i]);
      }
      if (T[i] === '#') {
        t.pop();
      } else if (T[i]) {
        t.push(T[i]);
      }
    }
    return t.join() === s.join();
};

复杂度分析

  • 时间复杂度——O(n)
  • 空间复杂度——O(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant