LeetCode 844.比较含退格的字符串
通过栈实现:
1.判断是否为‘#',是则出栈,否则入栈
2.对比两个栈
class Solution {
public:
void transform(string S, stack<char> &s){
for(int i = 0; i< S.size(); i++){
if(S[i] == '#' && !s.empty()) s.pop();
else if(S[i] != '#') s.push(S[i]);
}
return;
}
bool backspaceCompare(string S, string T) {
stack<char> s;
stack<char> t;
transform(S,s);
transform(T,t);
if(s.size() - t.size()) return false;
while(!s.empty()){
if(s.top() != t.top()) return false;
s.pop(),t.pop();
}
return true;
}
};