Problems
Parentheses Balancing Check
-
Use a stack for parenthesis storage
-
Push in case of an opening one
-
Pop in case of a closed one but only when the stack is not empty and the head is an opening one
-
Run through until the end, balanced if stack is empty
-
Use empty list for an early exit
// Scala def balance(chars: List[Char]): Boolean = { def _balance(list: List[Char], stack: List[Char]): Boolean = { list match { case Nil => stack.isEmpty case '(' :: cs => _balance(cs, '(' :: stack) case ')' :: cs => if (!stack.isEmpty && stack.head == '(') _balance(cs, stack.tail) else _balance(Nil, ')' :: stack) case _ :: cs => _balance(cs, stack) } } _balance(chars, Nil) }