๐Day 6 โ Teacher's Day Challenge | Valid Parentheses | Stack + Hash Map


๐๏ธ Date: July 30, 2025
๐ Challenge:
๐ข Problem: Valid Parentheses โ LeetCode #20 (Easy)
๐ฏ Topic: Stack, Hash Map, String
๐ป Platform: Leetcode
๐ Table of Contents
Daily Summary
โ
Problem Statement (Summary):
Check if a given string of parentheses ()[]{}
is valid. A string is valid if:
Open brackets are closed by the same type.
Brackets are closed in the correct order.
Every closing bracket has a matching opening bracket.
๐งพ Examples:
"()"
โtrue
"()[]{}"
โtrue
"(]"
โfalse
"([])"
โtrue
"([)]"
โfalse
๐ง What I Learned Today:
Use a stack to keep track of open brackets.
Use a hash map to match closing โ opening brackets.
Push when open bracket, check+pop when closing.
Edge case: stack must be empty at end for a valid string.
๐งช Code (C++):
class Solution {
public:
bool isValid(string s) {
unordered_map<char, char> pairs = {
{')', '('}, {']', '['}, {'}', '{'}
};
stack<char> st;
for (char ch : s) {
if (ch == '(' || ch == '{' || ch == '[') {
st.push(ch);
} else {
if (st.empty() || st.top() != pairs[ch]) {
return false;
}
st.pop();
}
}
return st.empty();
}
};
๐ Time Complexity: O(n)
๐ฆ Space Complexity: O(n)
๐ธ LeetCode Submission Screenshot:
โ Key Takeaways:
Stack is ideal for tracking nested or matching structures.
HashMap simplifies pairing and matching logic.
Final check on
stack.empty()
ensures no unmatched opens remain.
๐ Daily Summary
Metric | Value |
Problems Solved | 1 |
Topics Covered | Stack, Matching Brackets |
Tools Used | C++ |
Next Focus | Stack + Queue Pattern Problems |
Day | 6 / 30 |
๐๏ธ Tags:#leetcode #43DaysChallenge #stack #hashmap #dsa #cpp #validparentheses
Subscribe to my newsletter
Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
