Hello, I was trying the following code in Valid Parentheses problem using Kotlin as language:
class Solution {
fun isValid(s: String): Boolean {
val symbolsMap = mapOf(
')' to '(',
']' to '[',
'}' to '{'
)
val stack = mutableListOf<Char>()
var i = 0
val symbols = s.toCharArray()
var symbol: Char
while (i < symbols.size) {
symbol = symbols[i]
// this is a new expression start... eg. (A + B)
if (symbolsMap.values.contains(symbol)) {
stack.add(symbol)
} else {
// closing parenthesis can't be a start of expression
if (stack.isEmpty()) {
return false
}
// not a pair
if (symbolsMap[symbol] != stack[stack.size - 1]) {
return false
}
stack.removeLast()
}
i++
}
return stack.isEmpty()
}
}And the compiler is throwing the error:
Line 27: Char 23: error: unresolved reference: removeLast
stack.removeLast()It should not throw this error because I am declaring the stack variable as type MutableList<E> which has the removeLast function as one of its functions:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/
Thanks