Advertisement
Guest User

Untitled

a guest
Sep 21st, 2012
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.47 KB | None | 0 0
  1. def balance(chars: List[Char]): Boolean = {
  2.   def innerBalance(chars: List[Char], summation: Int): Int = {
  3.     if (chars.isEmpty)
  4.       summation
  5.     else if (chars.head == '(')
  6.       innerBalance(chars.tail, summation + 1)
  7.     else if (chars.head == ')')
  8.       if (summation <= 0)
  9.         -1
  10.       else
  11.         innerBalance(chars.tail, summation - 1)
  12.      else
  13.         innerBalance(chars.tail, summation)
  14.   }
  15.   if (innerBalance(chars, 0) == 0)
  16.     true
  17.   else false
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement