- No, it's clear from problem description.
Input:
Output:
- False cases:
}{
,{{}
,{}}
,
fun isValid(s: String): Boolean {
val mapping = mapOf(')' to '(', ']' to '[', '}' to '{')
val stack = java.util.Stack<Char>()
for (c in s) {
if (mapping.containsKey(c)) {
// Only encounter right parenthese
if (stack.isEmpty()) return false
val left = stack.pop()
if (mapping[c] != left) {
return false
}
} else {
stack.push(c)
}
}
return stack.isEmpty()
}
Mind the case:
}}{{
(right parentheses first)