844. Backspace String Compare

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
string process(string s){
string ret ;
for(char c:s){
if(c=='#'){
if(!ret.empty()) ret.pop_back();
}
else ret+=c;
}
return ret;
}
bool backspaceCompare(string s, string t) {
string ret, ans;
string a = process(s), b = process(t);
return a==b;
}
};

analysis

  • time complexity O(n)
  • space complexity O(n)