Advertisement
Guest User

Untitled

a guest
Aug 19th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.53 KB | None | 0 0
  1.     def balance(chars: List[Char]): Boolean = {
  2.       def isBalanced(chars: List[Char], x: Int = 0, lastChar: Char = '#'): Boolean ={
  3.         if (chars.isEmpty)
  4.           if (lastChar == ')' && x == 0)
  5.             return true
  6.           else
  7.             return false
  8.         if (chars.head == '(')
  9.           isBalanced(chars.tail, x+1, chars.head)
  10.         else if (chars.head == ')')
  11.           isBalanced( chars.tail, x-1, chars.head)
  12.         else
  13.           isBalanced(chars.tail, x, lastChar)
  14.       }
  15.       isBalanced(chars)
  16.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement