Skip to content

Instantly share code, notes, and snippets.

@danwang
Created April 10, 2020 04:59
Show Gist options
  • Save danwang/1eafdf3b385b00f3198c2dd4c5856be5 to your computer and use it in GitHub Desktop.
Save danwang/1eafdf3b385b00f3198c2dd4c5856be5 to your computer and use it in GitHub Desktop.
BackspaceStringCompare.scala
object Solution {
def backspaceCompare(left: String, right: String): Boolean = {
evaluate(left) == evaluate(right)
}
def evaluate(s: String, index: Int = 0, soFar: List[Char] = Nil): List[Char] = {
if (index >= s.length) {
soFar
} else {
val char = s.charAt(index)
val nextList = (char, soFar) match {
case ('#', Nil) => Nil
case ('#', _ :: l) => l
case (_, _) => char :: soFar
}
evaluate(s, index + 1, nextList)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment