Level
Find the error
-
def is_valid(s):
-
    stack = []
-
    mapping = {')': '(', ']': '[', '}': '{'}
-
    for char in s:
-
        if char in mapping:
-
            top_element = stack.pop() if stack else '#'
-
            if mapping[char] != top_element:
-
                return False
-
        else:
-
            stack.append(char)
-
    return len(stack) == 0
-
print(is_valid('()[]{}'))

LeetCode 20: Valid Parentheses

In this LeetCode-style problem, you need to implement a function that determines if a string has valid parentheses ordering. Identify the line with a logical error and select the correct implementation to fix it. Pay attention to how the stack is used and how edge cases (like an empty stack) are handled.