Created
April 7, 2013 16:34
-
-
Save mylons/5331199 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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