class Solution {
public boolean backspaceCompare(String S, String T) {
int a = S.length() - 1;
int b = T.length() - 1;
int aa = 0, bb = 0;
while (a >= 0 || b >= 0) {
while (a >= 0) {
if (S.charAt(a) == '#' ) {
aa++;
a--;
} else {
if(aa > 0){
aa--;
a--;
}else if(aa == 0){
break;
}
}
}
while (b >= 0) {
if (T.charAt(b) == '#') {
bb++;
b--;
} else {
if(bb > 0){
bb--;
b--;
}else if(bb == 0){
break;
}
}
}
if (a >= 0 && b >= 0) {
if (S.charAt(a) != T.charAt(b)) {
return false;
}
} else {
if (a >= 0 || b >= 0) {
return false;
}
}
a--;
b--;
}
return true;
}
}
08-20
503
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)