Valid Parentheses β
LeetCode #20 π’ Easy Python3 Solve by @noeyislearningProblem Statement β
Given a string s containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input:
s = "()"
Output:true
Example 2:
Input:
s = "()[]{}"
Output:true
Example 3:
Input:
s = "(]"
Output:false
Constraints:
1 <= s.length <= 10^4
s
consists of parentheses only'()[]{}'
.
Solution β
python
class Solution:
def isValid(self, s: str) -> bool:
stack = []
bracket_map = {"(": ")", "[": "]", "{": "}"}
for char in s:
if char in bracket_map:
stack.append(char)
elif not stack or bracket_map[stack.pop()] != char:
return False
return not stack
Explanation β
Imagine you have a big party where only special guests can come in pairs:
- Round guests:
(
and)
- Square guests:
[
and]
- Curly guests:
{
and}
Your job is to be the doorman and make sure these rules are followed:
- (Pairs only) Everyone needs a matching partner to enter.
- (Right order) If a round guest enters first, a round guest has to be the one leaving. You can't have a square guest leave when the party is waiting for a round guest!
- (No leftovers) veryone has to have a partnerβno one left alone at the end.
Then your code is like a super-smart checklist for the doorman:
- (The Guest Stack) Imagine a special stack of plates. You'll put a plate on the stack every time an opening guest (
(
,[
, or{
) arrives. - (The Guest Guidebook) The
bracket_map
is like a party guidebook. It tells you who is the perfect partner for each opening guest. - (Checking the Line) For every guest in the line (
for char in s:
):- (New Guest) If it's an opening guest (if char in bracket_map:), add their plate to the stack.
- (Leaving Guest) If it's a closing guest:
- (Empty Party?) If the stack is empty (
not stack
), someone is trying to leave when no one's inside! That's not allowed. - (Wrong Partner?) Peek at the top plate (
stack.pop()
). Does the guidebook say they're a match (bracket_map[stack.pop()] != char
)? If not, someone broke the order.
- (Empty Party?) If the stack is empty (
- (Party Over?) At the end, if the stack is empty (return not stack), that means everyone had a partner and the party followed the rules!
More Examples β
Example 1 β s = "()[]{}"
@noeyislearning
(
arrives, put a round plate on the stack.)
arrives, matches the top plate! Take the plate away.[
arrives, put a square plate on the stack.]
arrives, matches the top plate! Take it away.{
arrives, put a curly plate on the stack.}
arrives, matches the top plate! Take it away.- Party's over, and the stack is empty! The party followed the rules!
Example 2 β s = "([)]"
@noeyislearning
(
arrives, put a round plate on the stack.[
arrives, put a square plate on the stack.]
arrives, matches the square plate! Take it away.)
arrives, but the top plate is round! They tried to leave in the wrong order. The party didn't follow the rules!