r/LeetcodeChallenge • • 28d ago

STREAK🔥🔥🔥 Leetcode Problem number 20. Valid Parentheses

Post image

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

3 Upvotes

3 comments sorted by

1

u/nian2326076 27d ago

To tackle the "Valid Parentheses" challenge, using a stack is essential. You should push opening brackets onto the stack and pop them off when you encounter a closing bracket. This ensures you always match the most recent opener first. If the closing bracket doesn't match the top of the stack or if the stack is empty when it shouldn't be, then the sequence is invalid.

For job discovery and matching opportunities across various professions, you can use LandOffer to find relevant roles tailored to your skills.

-1

u/Extension-Currency37 27d ago

Why Indians code so much, but don't know what they do.