Skip to content

Instantly share code, notes, and snippets.

@ctide
Forked from mylons/gist:5331199
Created April 7, 2013 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctide/5331208 to your computer and use it in GitHub Desktop.
Save ctide/5331208 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)
(left - right) == 0
else {
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