class Solution {
public:
bool backspaceCompare(string S, string T)
{
stack<int> s;
stack<int> t;
for(int i = 0;i < S.size();i++)
{
if(S[i] != '#')
s.push(S[i]);
else if(!s.empty())
s.pop();
}
for(int i = 0;i < T.size();i++)
{
if(T[i] != '#')
t.push(T[i]);
else if(!t.empty())
t.pop();
}
while(!s.empty())
{
if(t.empty())
return false;
else
{
if(s.top() == t.top())
{
s.pop();
t.pop();
}
else
return false;
}
}
if(t.empty())
return true;
else
return false;
}
};
算法思路:用两个栈分别将两个的最简式找出来,然后将两个栈进行对比。但是空间复杂度达不到进阶的要求,