Generate All Strings With n Matched Parentheses - Backtracking ('Generate Parentheses' on LeetCode)

preview_player
Показать описание
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions

Question: Write a program that takes as input a number n in and returns all the strings with n matched pairs of parens.

Examples:

n = 1
[ "()" ]

n = 2
[ "(())", "()()" ]

n = 3
[ "((()))","(()())","(())()","()(())","()()()" ]

Approach 1 (Brute Force Then Validate)

Generate all (2 ^ (2n)) possible parenthese strings and then validate each for being balanced.

If n = 2 then the string length will be 2 times that since all open parentheses are matched by closed parentheses.

This lower bounds our time complexity.

Even if we restrict the enumeration to just sets with an equal number of left and right parentheses we will have choose(2k, k) strings to consider for validation.

Approach 2 (Directed Backtracking)

The 3 Keys To Backtracking

Our Choice:
Whether we place a left or right paren at a certain decision point in our recursion.

Our Constraints:
We can't place a right paren unless we have left parens to match against.

Our Goal:
Place all k left and all k right parens.

The Key

At each point of constructing the string of length 2k we make a choice.

We can place a "(" and recurse or we can place a ")" and recurse.

But we can't just do that placement, we need 2 critical pieces of information.

The amount of left parens left to place.
The amount of right parens left to place.

We have 2 critical rules at each placement step.

We can place a left parentheses if we have more than 0 left to place.

We can only place a right parentheses if there are left parentheses that we can match against.

We know this is the case when we have less left parentheses to place than right parentheses to place.

Once we establish these constraints on our branching we know that when we have 0 of both parens to place that we are done, we have an answer in our base case.

++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++

This question is number 16.6 in "Elements of Programming Interviews" by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.
Рекомендации по теме
Комментарии
Автор

Table of Contents:

Introduction: Discussing The Past 0:00 - 0:33
Talking About The Implications Of Today's Video 0:33 - 1:08
The Problem Introduction 1:08 - 3:00
The 3 Keys To Backtracking (As Always) 3:00 - 5:01
Tracing The Recursion Tree: Watch The States 5:01 - 10:16
Time Complexity 10:16 - 10:48
Space Complexity 10:48 - 11:19
Subscribe To Us Plz 11:19 - 11:41

The code is in the description. Fully commented for teaching purposes and understanding.

BackToBackSWE
Автор

You are an awesome teacher. Please keep making these videos.

theFifthMountain
Автор

When he speaks, It feels like he is about to get inside my brain.

mohammedshahraazhussain
Автор

What you said in the beginning resonated a lot with my experiences. Most videos out there explaining these solutions don't dive into the intuition behind making certain logical leaps for coming up with the solution, which is probably the crux of the difficulty. Keep up the great work.

gzfhvoe
Автор

“This is the understanding I want you to understand!”
Such a awesome video

sean
Автор

You are the best teacher on entire Youtube platform. No one have explained any problem in such an expressive way [ The way you teach ]. Thanks for your tutorials. Please don't stop teaching.

hackytech
Автор

0:08 Another day another boring question to cover.
1:08 Aoolrite so today we have a fascinating question.

redherring
Автор

Dude, I seriously cannot believe how much I struggled with this problem until I saw your thinking behind it. I typed up a solution in less than 5 mins. You are seriously a beast. Bless your soul man.

Автор

I guess this is the BEST CHANNEL FOR SOFTWARE INTERVIEWS!

SR-wevl
Автор

Hated this problem but this is the best explanation on YouTube about how to solve it. Thank you.

mirceskiandrej
Автор

Your ability to completely explaining crux of problem is amazing.

vishwajeetmanhas_
Автор

I can't believe how easily I understood the solution after I watched your video. I can't believe how easily I've written the actual code without looking any example code but only watching your video. Thank you so much!

hernabaer
Автор

You're the best teacher ever, and I'm being VERY honest. You have helped me understand concepts I used to struggle with

ChideraAbaraonye
Автор

Thanks! Struggled on this problem for a bit, but after 5 minutes of the video I was able to come up with the right answer.

fudgemelons
Автор

I've watched countless algorithm explanation videos.. yours are by far my favorite!! I can actually understand the how & why to the approach instead of just learning this is what you do. Thank youuu

coolsuck
Автор

I love your videos man. They are the greatest thing to have happened on YouTube since 30 Seconds to Mars.

mosesindecks
Автор

Thanks. Understood the solution literally within 2 mins (I had spent a decent amount of time on this before).

coconutjuice
Автор

dude this is literally the type of explaination I have looked for. So many other videos just gave a soloution and didnt really explain wtf was happenig. You are wonderful

grantherman
Автор

My favorite explanation yet. Very satisfying when I realized how easy this problem is.

johnleonardo
Автор

I've seen a lot of explanations about this problem but this is so far the best explanations I've seen about using backtracking, thanks!

juliolopezmontalvo