code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

01 Backspace String Compare

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const backspaceStringCompare = (s, t) => {
  let sPointer = s.length - 1
  let tPointer = t.length - 1
  let sBackspaceCount = 0
  let tBackspaceCount = 0
  while (sPointer >= 0 || tPointer >= 0) {
    if (sBackspaceCount == 0 && tBackspaceCount == 0) {
      if (s[sPointer] !== '#' && t[tPointer] !== '#') {
        if (s[sPointer] !== t[tPointer]) {
          return false
        }
        sPointer -= 1
        tPointer -= 1
      } else {
        if (s[sPointer] == '#') {
          sBackspaceCount += 1
          sPointer -= 1
        }
        if (t[tPointer] == '#') {
          tBackspaceCount += 1
          tPointer -= 1
        }
      }
    } else {
      while (sBackspaceCount != 0) {
        if (s[sPointer] == '#') {
          sBackspaceCount += 1
        } else {
          sBackspaceCount -= 1
        }
        sPointer -= 1
      }
      while (tBackspaceCount != 0) {
        if (t[tPointer] == '#') {
          tBackspaceCount += 1
        } else {
          tBackspaceCount -= 1
        }
        tPointer -= 1
      }
    }
  }
  return true
}

console.log(backspaceStringCompare('ab##', 'a#d#'))

Created 2022-02-04T04:03:39+00:00 · Edit