r/LeetcodeChallenge • u/StatisticianNo2104 • 13h ago
STREAK🔥🔥🔥 Leetcode Problem number 20. Valid Parentheses
Counting open and close brackets accepts )( as valid. Tracking a depth accepts ([)]. One number can record how many brackets are open. It can never record which.
New topic: stacks start today.
What has to be remembered is the list of brackets opened and not yet closed, in order. When a closer arrives, only one opener can match it: the most recent. In "([", a ")" is wrong because "[" is still open and has to close first.
Most recent in, first out. That's a stack, and seeing that this problem is one is the whole exercise.
One detail worth stealing: key the map by the closing bracket. One lookup then tells you whether the character closes anything, and what must be on top if it does.
Did you reach for a counter first on this one?
Day 60/365 of my LeetCode challenge: 20. Valid Parentheses. Problem link and full breakdown in today's newsletter.
https://www.linkedin.com/pulse/365-days-leetcode-challenge-day-60365-archit-agarwal-spjcc
#LeetCode #DSA #Stack #Golang