The problem looks simple at first, but it is a great example of how choosing the right data structure can make a solution both clean and efficient.
The goal is to determine whether a string containing (), {}, and [] is valid. Every opening bracket must have the correct closing bracket, and brackets must close in the proper order.
I solved it using a Stack. The approach is straightforward:
- Create an empty stack.
- Store matching closing brackets for each opening bracket.
- Traverse the string character by character.
- If the character is an opening bracket, push it onto the stack.
-
If it is a closing bracket, remove the most recent opening bracket using
pop(). - Check whether the opening and closing brackets match.
-
If they don't match, immediately return
false. - At the end, the stack must be empty for the string to be valid.
This problem reinforced an important concept: Stacks are extremely useful whenever the most recently added element needs to be processed first (LIFO – Last In, First Out).
My submission achieved 0 ms runtime and passed 103/103 test cases. 💻🔥
Small problems like this are great for strengthening fundamental data-structure and problem-solving skills. One problem at a time, one concept at a time! 🚀
Tags:
#LeetCode #JavaScript #DSA #DataStructures #Algorithms #Stack #ValidParentheses #Coding #Programming #ProblemSolving #100DaysOfCode #CodingJourney #Developer #WebDevelopment #SoftwareEngineering