Skip to content

Instantly share code, notes, and snippets.

@mamalisk
mamalisk / balanced.scala
Last active August 9, 2016 14:55
Scala : Determine whether parentheses are balanced within a string
def balance(chars: List[Char]): Boolean = {
val onlyBrackets = chars.filter(c => '('.equals(c) || ')'.equals(c))
@tailrec
def isBalanced(status : Int, chars : List[Char]) : Boolean = {
if(chars.isEmpty) status == 0
else status >= 0 && isBalanced({if(chars.head == ')') status - 1 else status + 1}, chars.tail)
}