Kotlin - Completed BrackedPush

This commit is contained in:
Anthony C 2017-08-01 15:27:44 -04:00
parent 3b643db419
commit 5267c1fd88

View file

@ -0,0 +1,32 @@
class BracketPush {
companion object {
fun isValid(inpString: String): Boolean{
val stack = mutableListOf<Char>()
val realString = inpString.filter {
(it == '{') or
(it == '[') or
(it == '(') or
(it == ')') or
(it == ']') or
(it == '}') }
for (i in realString){
if ((i == '{') or
(i == '[') or
(i == '(')){
stack.add(0, i)
}
else {
val temp = if (stack.size == 0) {'a'} else {stack.removeAt(0)}
when (temp){
'(' -> if (i != ')') return false
'{' -> if (i != '}') return false
'[' -> if (i != ']') return false
else -> return false
}
}
}
return stack.isEmpty()
}
}
}