Advertisement
Guest User

Untitled

a guest
May 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.48 KB | None | 0 0
  1. def balance(chars: List[Char]): Boolean = {
  2.  
  3.     def balanceInner(chars: List[Char], acc: Int = 0): Boolean = {
  4.       def accUpgrade(character: Char) = character match {
  5.         case '(' => acc + 1
  6.         case ')' => acc + -1
  7.         case _ => acc
  8.       }
  9.  
  10.       if (acc < 0) false else chars.isEmpty match {
  11.         case true => if (acc == 0) true else false
  12.         case false => balanceInner(chars.tail, accUpgrade(chars.head))
  13.       }
  14.     }
  15.  
  16.     balanceInner(chars)
  17.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement