Skip to content

Latest commit

 

History

History
35 lines (31 loc) · 819 Bytes

20.valid-parentheses.md

File metadata and controls

35 lines (31 loc) · 819 Bytes

Clarification Questions

  • No, it's clear from problem description.

Test Cases

Normal Cases

Input: 
Output: 

Edge / Corner Cases

  • 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)