Skip to content

Instantly share code, notes, and snippets.

@mylons
Created April 7, 2013 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mylons/5331199 to your computer and use it in GitHub Desktop.
Save mylons/5331199 to your computer and use it in GitHub Desktop.
def balance(chars: List[Char]): Boolean = {
val l = '('
val r = ')'
def helper(left: Int, right: Int, c: List[Char]): Boolean = {
if (c.isEmpty) return (left - right) == 0
if (right > left) false
else if (c.head == l) helper(left + 1, right, c.tail)
else if (c.head == r) {
helper(left, right + 1, c.tail)
} else {
helper(left, right, c.tail)
}
}
helper(0,0,chars)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment