exercism/kotlin/bracket-push/src/main/kotlin/BracketPush.kt

32 lines
No EOL
994 B
Kotlin

import java.util.Stack
class BracketPush {
companion object {
fun isValid(inpString: String): Boolean{
val stack = Stack<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.push(i)
}
else {
val temp = if (stack.size == 0) {'a'} else {stack.pop()}
when (temp){
'(' -> if (i != ')') return false
'{' -> if (i != '}') return false
'[' -> if (i != ']') return false
else -> return false
}
}
}
return stack.isEmpty()
}
}
}