Valid Parentheses Problem Solved with Stack

🚀 Day 31 of #100DaysOfLearning | #100DaysOfCode 🧿 Today I solved a classic and beginner-friendly problem, but with a very powerful concept behind it — Valid Parentheses (LeetCode 20). At first glance, it looks simple: Just check whether (), {}, and [] are balanced. But this problem is actually a perfect introduction to how Stack works in real scenarios. 🧩 Problem Understanding We are given a string containing only: ( ) { } [ ] The string is valid if: Every opening bracket has a matching closing bracket. Brackets are closed in the correct order. No closing bracket appears without its correct opening one. Examples: "()" → ✅ Valid "()[]{}" → ✅ Valid "(]" → ❌ Invalid "([)]" → ❌ Invalid 🛠️ Approach Using Stack I used the Stack Pattern: Traverse the string character by character. If it’s an opening bracket → push it into the stack. If it’s a closing bracket: If stack is empty → invalid. Check the top of stack: If it doesn’t match the closing bracket → invalid. Else → pop the top. At the end: If stack is empty → valid. Otherwise → invalid. This feels like real life: Every “open” action must be “closed” in the correct order. Stack naturally enforces this rule. 🧠 What I Learned Today Stack is perfect when we need to: Track previous state, Match pairs, Maintain order Even “easy” problems build the foundation for: Expression evaluation, Compiler parsing, Syntax checking Clean logic > complicated brute force. 📌 Day 31 Complete Even though the problem was easy, it strengthened my understanding of stack-based validation. Small steps like this are what build big confidence over time. 💬 “Master the basics, and the advanced problems will start feeling natural.” #Day31 #100DaysOfCode #100DaysOfLearning #Stack #DSA #LeetCode #ProblemSolving #CodingJourney #Consistency #LearnEveryDay

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories