Advertisement
Guest User

Untitled

a guest
May 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.45 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): Int =
  5.         if (character == '(') acc + 1
  6.         else if (character == ')') acc - 1
  7.         else acc
  8.  
  9.       if (acc < 0 ) false
  10.       else if (chars.isEmpty)
  11.         if (acc == 0)true else false
  12.       else balanceInner(chars.tail, accUpgrade(chars.head))
  13.     }
  14.  
  15.     balanceInner(chars)
  16.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement