I got a assignment for balancing parentheses using scala. I wrote this code:
def balance(chars: List[Char]): Boolean = {
def check(sent: List[Char], count: Int): Int =
if (sent.isEmpty)
count
else if (sent.head == '(')
check(sent.tail, count + 1)
else if (sent.head == ')')
check(sent.tail, count - 1)
else
check(sent.tail, count)
check(chars, 0) == 0 }
but this code fails in "())("
any idea to implement this code correct?