BF 165. remove characters

通常刪字的都是 Stack

bigfrontend

Given a string contaning only a, b and c, remove all b and ac.

removeChars('ab') // 'a'
removeChars('abc') // ''
removeChars('cabbaabcca') // 'caa'
```javascript
/**
 * @param {string} input
 * @returns string
 */
function removeChars(input) {
   let stack = []
   for(let i = input.length-1; i>=0; i--){
    if(input[i] === "a" && stack[0] === "c"){
      stack.shift()
      
    } else if(input[i] !== "b"){
      stack.unshift(input[i])
    }
  }

  return stack.join("")
}

```

怎麼解?

先把需要用蛋目前未用的 "c" 存進 stack

Last updated