You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
639 B
TypeScript

function findNotSkipedIndex(s: string, i: number) {
let skipCount = 0;
while (i >= 0 && (skipCount > 0 || s[i] === '#')) {
if (s[i] === '#') {
skipCount += 1;
i -= 1;
continue;
}
skipCount -= 1;
i -= 1;
}
return i;
};
export function backspaceCompare(s: string, t: string): boolean {
let sI = s.length;
let tI = t.length;
while (sI > 0 && tI > 0) {
sI = findNotSkipedIndex(s, sI - 1);
tI = findNotSkipedIndex(t, tI - 1);
if (sI >= 0 && tI >= 0 && s[sI] !== t[tI]) {
return false;
}
}
return findNotSkipedIndex(s, sI - 1) === findNotSkipedIndex(t, tI - 1);
};