Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
SHLOK KOTHARI
Understanding the greedy aspect of the question

SHLOK KOTHARI

Jul 3, 2024

I am not completely sure how this problem employs a greedy strategy, but here is my understanding. As soon as the number of closed brackets exceeds the number of open brackets, we plan to handle it. Even if there is only one mismatch, we increase the counter and reduce the balance. This approach works for this problem because traversing from the beginning ensures we have all the information about the preceding brackets, which is sufficient to obtain the solution at the current position.

Let me know if I am thinking wrong here. Thanks!

0

0

Comments
Comments
Shubham Vora
Shubham Voraa year ago

For each character in the string, if it's an opening parenthesis '(', we increase the balance. If it's a closing parenthesis ')', we decrease the balance. If the balance is negative at any point (which means there are more closing parentheses than opening ones), we incr...

On this page