Valid Parentheses
LeetCode 20 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Push openers; each closer must match the top.
நான் Valid Parentheses (LeetCode 20) ஐ இப்படித் தீர்த்தேன் — Parentheses / Bracket Matching பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Every closing bracket must match the most recent unmatched opening bracket — that's last-in-first-out, a stack.
Approach
- 1
Opening bracket → push.
- 2
Closing bracket → pop and check it matches; empty stack → invalid.
- 3
Valid only if the stack is empty at the end.
நான் எப்படித் தீர்த்தேன்
Stack matchingPythonO(n) time · O(n) space
class Solution:
def isValid(self, s: str) -> bool:
stack = []
mpp={
"(":")",
"{":"}",
"[":"]"
}
stack = []
for char in s:
if char in mpp:
stack.append(char)
else:
closing_brace = char
if stack:
opening_brace = stack.pop()
if mpp[opening_brace]!=closing_brace:
return False
else:
return False
return True if len(stack)==0 else FalseValid Parentheses — அடிக்கடி கேட்கப்படும் கேள்விகள்
Valid Parentheses பின்னாலுள்ள intuition என்ன?
Every closing bracket must match the most recent unmatched opening bracket — that's last-in-first-out, a stack.
Valid Parentheses (LeetCode 20) ஐ எப்படித் தீர்ப்பது?
1. Opening bracket → push. 2. Closing bracket → pop and check it matches; empty stack → invalid. 3. Valid only if the stack is empty at the end.
Valid Parentheses இன் time மற்றும் space complexity என்ன?
Time: O(n). Space: O(n). (Accepted approach: Stack matching.)
Valid Parentheses இல் பொதுவான தவறுகள் என்ன?
Leftover openers like '((' make it invalid.