[LeetCode] 844. Backspace String Compare
풀이
#include <stack>
class Solution {
public:
bool backspaceCompare(string s, string t) {
stack<char> sStack;
for(int i = 0; i<s.length(); ++i){
if(s[i] != '#')
sStack.push(s[i]);
else if(!sStack.empty()) sStack.pop();
}
stack<char> tStack;
for(int i = 0; i<t.length(); ++i){
if(t[i] != '#')
tStack.push(t[i]);
else if(!tStack.empty()) tStack.pop();
}
if(sStack.size() != tStack.size()) return false;
while(!sStack.empty()){
if(sStack.top() != tStack.top()) return false;
sStack.pop();
tStack.pop();
}
return true;
}
};