Skip to content

Instantly share code, notes, and snippets.

@gsriram7
gsriram7 / parenthesis
Created May 18, 2015 07:07
Here is my solution for exercise #2 using recursion and pattern matching.. Easy to understand
def parenthesis(input: List[Char], count: Int):Boolean ={
input match {
case Nil if count==0 => true
case Nil if count!=0 => false
case head :: tail if count==(-1) => false
case head :: tail if head == '('=> parenthesis(tail, count + 1)
case head :: tail if head == ')'=> parenthesis(tail, count - 1)
case head :: tail if head!='(' && head!=')' => parenthesis(tail, count)
}
}