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);
};
