/*
* @lc app=leetcode.cn id=844 lang=typescript
*
* [844] 比较含退格的字符串
*/
//双指针法
// @lc code=start
function backspaceCompare(s: string, t: string): boolean {
let sfast = s.length - 1,
sslow = 0,
tfast = t.length - 1,
tslow = 0;
while (sfast >= 0 || tfast >= 0) {
//找s中的#
while (sfast >= 0) {
if (s[sfast] === "#") {
sslow++;
} else {
if (sslow > 0) {
sslow--;
} else {
break;
}
}
sfast--;
}
//找t中的#
while (tfast >= 0) {
if (t[tfast] === "#") {
tslow++;
} else {
if (tslow > 0) {
tslow--;
} else {
break;
}
}
tfast--;
}
//比较
if (s[sfast] !== t[tfast]) {
return false;
}
sfast--;
tfast--;
}
return true;
}
// @lc code=end