11. Generate Parantheses | The recursion question that I have asked the most in interviews! 🚀🚀

Sdílet
Vložit
  • čas přidán 3. 08. 2022
  • You can practise the question here-
    bit.ly/3zu7fah
    𝐃𝐨𝐧'𝐭 𝐜𝐥𝐢𝐜𝐤 𝐡𝐞𝐫𝐞- bit.ly/3PCiXWD
    Subscribe and hit the notification icon to get notifications and learn with me everyday!! ❤️❤️
    #dsa #softwaredeveloper #interviewpreparation

Komentáře • 54

  • @subhranilsamanta2862
    @subhranilsamanta2862 Před 2 lety +1

    🙏🏽 for being consistent. Keep up the great work.

  • @abirchakroun6095
    @abirchakroun6095 Před rokem +2

    i started with the series today, did all the videos and after 5min in this one i managed to code it alone 🙌 you have such an amazing explanation and teaching skills thanks !

  • @bhaumikdedhia1998
    @bhaumikdedhia1998 Před 2 lety +1

    You've explained this in such a simple way!! :)

  • @arunkumarmohandas4420
    @arunkumarmohandas4420 Před 2 lety +2

    Present. Paused at 1:48. And attempted the java code for this.Could get all testcases passed in GFG page:
    public class PrintValidCombinationOfParanthesises {
    public static void main (String args[]){
    PrintValidCombinationOfParanthesises printValidCombinationOfParanthesises = new
    PrintValidCombinationOfParanthesises();
    System.out.println(printValidCombinationOfParanthesises.AllParenthesis(3));
    }
    public List AllParenthesis(int n)
    {
    List resultOfValidParanthesis = new ArrayList();
    int numberOfStartingParanthesises = n;
    int numberOfEndingParanthesises = n;
    generateParanthesis(numberOfStartingParanthesises,
    numberOfEndingParanthesises,
    "",
    resultOfValidParanthesis);
    return resultOfValidParanthesis;
    }
    void generateParanthesis(int numberOfStartingParanthesisesLeft,
    int numberOfEndingParanthesisesLeft,
    String stagingStr,
    List resultOfValidParanthesisresultOfValidParanthesis)
    {
    // Exit condition - if number of start paranthesises and
    // number of end pranthesises is 0 , then there is
    // nothing left.
    // So add the 'stagingStr',
    // we have into the result list.
    // Actually we only need to check whether we have end pranthesis are left.
    // Because the logic below gurantees that starting paranthesises will
    // be used up before the ending pranthesises gets used up.
    // But keeping both checks for clarity
    if(numberOfStartingParanthesisesLeft==0
    && numberOfEndingParanthesisesLeft==0){
    resultOfValidParanthesisresultOfValidParanthesis.add(stagingStr);
    return;
    }
    // if there are any starting paranthesis left to be used,
    // first give importance to it and add it and then
    // start off a recursive branch
    if(numberOfStartingParanthesisesLeft>0){
    // add a starting pranthesis to the statging string.
    // We shud start off with start pranthesis
    // and never with the end tag.
    // this check also ensures that for every end paranthises that
    // will be added later up the stack,
    // ther will be a valid starting pranthesis
    // and then start off a recursive call branch by reducing 1 count
    // from starting paranthesis count
    generateParanthesis(numberOfStartingParanthesisesLeft-1,
    numberOfEndingParanthesisesLeft,
    stagingStr+"(",
    resultOfValidParanthesisresultOfValidParanthesis);
    }
    // if the number of end tags is greater than number of starting paranthesises,
    // then there is a chance to add an end paranthesis
    // (without violating the validity constraints)
    // and start off a recursive branch
    if(numberOfEndingParanthesisesLeft > numberOfStartingParanthesisesLeft){
    // add a ending pranthesis to the staging string
    generateParanthesis(numberOfStartingParanthesisesLeft,
    numberOfEndingParanthesisesLeft-1,
    stagingStr+")",
    resultOfValidParanthesisresultOfValidParanthesis);
    }
    }// end of generateParanthesis method

  • @koushikvss7638
    @koushikvss7638 Před 2 lety

    Very frequently asked question and great detailed explanation. Thank you!

  • @prakashbtw678
    @prakashbtw678 Před 2 lety +2

    I failed by this Question around 3 month back in interview. Thanks for clear explanation

  • @purvirawat1657
    @purvirawat1657 Před 2 lety

    Keerti mam , seriously your way of teaching is something miraculous ✨to me , it's now coming in catch ...dealing with recursive calls , recursive stack and base condition .

  • @shreehari2589
    @shreehari2589 Před 3 měsíci

    Awesome explanation please keep uploading more videos

  • @keshavgambhir9894
    @keshavgambhir9894 Před 2 lety

    Present Ma'am. Amazing Consistency.

  • @shashwatraghuwanshi4174

    Watcing your videos late...but I will follow the series for sure!!!

  • @manideepamara2263
    @manideepamara2263 Před 2 lety

    Today I understood the importance of state graph and how it make too easy in writing recursive solution.

  • @nidhibansaliitr
    @nidhibansaliitr Před 2 lety

    IMO, below code is easy then above approach.
    void helper(int n, String curr, int index, List res){
    if(index == n){
    res.add(curr);
    return;
    }
    String newCurr1 = curr+"()";
    String newCurr2 = "()"+curr;
    if(!newCurr1.equals(newCurr2))
    helper(n,newCurr2,index+1,res);
    helper(n,newCurr1,index+1,res);
    newCurr1 = "("+curr+")";
    helper(n,newCurr1,index+1,res);
    }
    public List AllParenthesis(int n)
    {
    // Write your code here
    List res = new ArrayList();
    String curr = "()";
    helper(n,curr,1,res);
    Collections.sort(res,null);
    return res;
    }
    We took one pair...next pair can be in front or last or entangle the current.
    On local this code is working fine with n=4...but GFG is showing only 8 entries in outcome where as local run is giving all 14 entries.
    Please share your feedback.

  • @user-gf9mw5in7l
    @user-gf9mw5in7l Před 5 měsíci

    thank you ma'am, solved this without referring solution once you explained the logic, so easy it was, thanks a lot ma'am!

  • @shafi_786
    @shafi_786 Před 8 dny

    Thank you keerti for wonderful explaination

  • @aravinds6406
    @aravinds6406 Před 4 měsíci

    Excellent explanation.

  • @nsjdeveloper1123
    @nsjdeveloper1123 Před 3 měsíci

    Didi thank you so much we are getting knowledge from you only thankyou guruji

  • @gunavathishanmugasundaram6599

    Good explanation 👏

  • @ankushladani496
    @ankushladani496 Před rokem

    Mam really explanation was awesome and now I can speak before you write that code this should be there.

  • @chandnibhatia1211
    @chandnibhatia1211 Před rokem +1

    Keerti , can we just add ( or () and add remaining ) in base condtion ..will that work

  • @basavarajsonnad5775
    @basavarajsonnad5775 Před 2 lety

    Too good explanation

  • @nidhikumari1479
    @nidhikumari1479 Před 2 lety

    Thank you so much mam! ❤

  • @ankushladani496
    @ankushladani496 Před rokem

    Thank you mam for this video....

  • @mohdhaseeb9818
    @mohdhaseeb9818 Před 2 lety

    Present Maaa'mm!!

  • @suryasahi9450
    @suryasahi9450 Před 5 měsíci

    bahut acha samjhaya

  • @srishtikapoor7261
    @srishtikapoor7261 Před rokem

    Thank you ❤

  • @hydrocy.9165
    @hydrocy.9165 Před 3 měsíci

    using + with string name adds parenthesis?

  • @pratikbahad9169
    @pratikbahad9169 Před 2 lety

    Present mam 🙌🏻

  • @cricketguru7596
    @cricketguru7596 Před 2 lety

    Present 👩‍💻🔥

  • @rtarts1524
    @rtarts1524 Před 2 lety

    Thank you mam...

  • @nithishnair6265
    @nithishnair6265 Před 2 lety

    Present 👍

  • @vigneshiyer1765
    @vigneshiyer1765 Před 2 lety

    present mam!!

  • @Nishchay-kc1gb
    @Nishchay-kc1gb Před 2 lety

    Do we have any other method to solve this problem so that time complexity is reduced

  • @deepchill5295
    @deepchill5295 Před 2 lety

    Day 24- Present 🙋‍♂️

  • @manindhra748
    @manindhra748 Před 2 lety

  • @surojitsantra7627
    @surojitsantra7627 Před 2 lety

    🙋. before watch sol tried that, able to solve it.

  • @manindhra748
    @manindhra748 Před 2 lety

    Mam unable to understand when to backtrack and when to not pls explain!!

    • @codefromscratch-keertipurswani
      @codefromscratch-keertipurswani  Před 2 lety

      I explained a bit in today’s video. Recommend you to go through 7,8 videos in recursion series again. If you still have doubt, let me know. I am here ❤️

    • @manindhra748
      @manindhra748 Před 2 lety

      @@codefromscratch-keertipurswani yes mam got clear now 🤗

  • @vamseekrishna3038
    @vamseekrishna3038 Před 2 lety

    🖐♥

  • @galepraveen9468
    @galepraveen9468 Před 2 lety

    samaj me aagaya didi, mereko samaj me aagaya

  • @jivanninawe3190
    @jivanninawe3190 Před 2 lety

    Mam kitne question honge aur ismay

  • @varunendra.singh_
    @varunendra.singh_ Před 2 lety

    Day 24🙋‍♂️

  • @nidhibansaliitr
    @nidhibansaliitr Před 2 lety

    IMO, my code is easy then above approach.
    void helper(int n, String curr, int index, List res){
    if(index == n){
    res.add(curr);
    return;
    }
    String newCurr1 = curr+"()";
    String newCurr2 = "()"+curr;
    if(!newCurr1.equals(newCurr2))
    helper(n,newCurr2,index+1,res);
    helper(n,newCurr1,index+1,res);
    newCurr1 = "("+curr+")";
    helper(n,newCurr1,index+1,res);
    }
    public List AllParenthesis(int n)
    {
    // Write your code here
    List res = new ArrayList();
    String curr = "()";
    helper(n,curr,1,res);
    Collections.sort(res,null);
    return res;
    }
    We took one pair...next pair can be in front or last or entangle the current.
    On local this code is working fine with n=4...but GFG is showing only 8 entries in outcome where as local run is giving all 14 entries.
    Please share your feedback.

  • @nidhibansaliitr
    @nidhibansaliitr Před 2 lety

    IMO, below code is easy then above approach.
    void helper(int n, String curr, int index, List res){
    if(index == n){
    res.add(curr);
    return;
    }
    String newCurr1 = curr+"()";
    String newCurr2 = "()"+curr;
    if(!newCurr1.equals(newCurr2))
    helper(n,newCurr2,index+1,res);
    helper(n,newCurr1,index+1,res);
    newCurr1 = "("+curr+")";
    helper(n,newCurr1,index+1,res);
    }
    public List AllParenthesis(int n)
    {
    // Write your code here
    List res = new ArrayList();
    String curr = "()";
    helper(n,curr,1,res);
    Collections.sort(res,null);
    return res;
    }
    We took one pair...next pair can be in front or last or entangle the current.
    On local this code is working fine with n=4...but GFG is showing only 8 entries in outcome where as local run is giving all 14 entries.
    Please share your feedback.

  • @nidhibansaliitr
    @nidhibansaliitr Před 2 lety

    IMO, below code is easy then above approach.
    void helper(int n, String curr, int index, List res){
    if(index == n){
    res.add(curr);
    return;
    }
    String newCurr1 = curr+"()";
    String newCurr2 = "()"+curr;
    if(!newCurr1.equals(newCurr2))
    helper(n,newCurr2,index+1,res);
    helper(n,newCurr1,index+1,res);
    newCurr1 = "("+curr+")";
    helper(n,newCurr1,index+1,res);
    }
    public List AllParenthesis(int n)
    {
    // Write your code here
    List res = new ArrayList();
    String curr = "()";
    helper(n,curr,1,res);
    Collections.sort(res,null);
    return res;
    }
    We took one pair...next pair can be in front or last or entangle the current.
    On local this code is working fine with n=4...but GFG is showing only 8 entries in outcome where as local run is giving all 14 entries.
    Please share your feedback.