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

Поділитися
Вставка
  • Опубліковано 30 лис 2024

КОМЕНТАРІ • 54

  • @subhranilsamanta2862
    @subhranilsamanta2862 2 роки тому +1

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

  • @abirchakroun6095
    @abirchakroun6095 2 роки тому +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 !

  • @prakashbtw678
    @prakashbtw678 2 роки тому +2

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

  • @shafi_786
    @shafi_786 4 місяці тому

    Thank you keerti for wonderful explaination

  • @bhaumikdedhia1998
    @bhaumikdedhia1998 2 роки тому +1

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

  • @thenameisafsal
    @thenameisafsal 9 місяців тому

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

  • @manideepamara2263
    @manideepamara2263 2 роки тому

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

  • @arunkumarmohandas4420
    @arunkumarmohandas4420 2 роки тому +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

  • @keshavgambhir9894
    @keshavgambhir9894 2 роки тому

    Present Ma'am. Amazing Consistency.

  • @nidhibansaliitr
    @nidhibansaliitr 2 роки тому

    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.

  • @purvirawat1657
    @purvirawat1657 2 роки тому

    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 .

  • @ankushladani496
    @ankushladani496 2 роки тому

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

  • @shreehari2589
    @shreehari2589 8 місяців тому

    Awesome explanation please keep uploading more videos

  • @koushikvss7638
    @koushikvss7638 2 роки тому

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

  • @chandnibhatia1211
    @chandnibhatia1211 2 роки тому +1

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

  • @shashwatraghuwanshi4174
    @shashwatraghuwanshi4174 2 роки тому

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

  • @aravinds6406
    @aravinds6406 8 місяців тому

    Excellent explanation.

  • @nsjdeveloper1123
    @nsjdeveloper1123 7 місяців тому

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

  • @mohdhaseeb9818
    @mohdhaseeb9818 2 роки тому

    Present Maaa'mm!!

  • @suryasahi9450
    @suryasahi9450 9 місяців тому

    bahut acha samjhaya

  • @hydrocy.9165
    @hydrocy.9165 7 місяців тому

    using + with string name adds parenthesis?

  • @ankushladani496
    @ankushladani496 2 роки тому

    Thank you mam for this video....

  • @gunavathishanmugasundaram6599
    @gunavathishanmugasundaram6599 2 роки тому

    Good explanation 👏

  • @basavarajsonnad5775
    @basavarajsonnad5775 2 роки тому

    Too good explanation

  • @deepchill5295
    @deepchill5295 2 роки тому

    Day 24- Present 🙋‍♂️

  • @nidhikumari1479
    @nidhikumari1479 2 роки тому

    Thank you so much mam! ❤

  • @srishtikapoor7261
    @srishtikapoor7261 2 роки тому

    Thank you ❤

  • @cricketguru7596
    @cricketguru7596 2 роки тому

    Present 👩‍💻🔥

  • @pratikbahad9169
    @pratikbahad9169 2 роки тому

    Present mam 🙌🏻

  • @rtarts1524
    @rtarts1524 2 роки тому

    Thank you mam...

  • @nithishnair6265
    @nithishnair6265 2 роки тому

    Present 👍

  • @manindhra748
    @manindhra748 2 роки тому

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

    • @codefromscratch-keertipurswani
      @codefromscratch-keertipurswani  2 роки тому

      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 2 роки тому

      @@codefromscratch-keertipurswani yes mam got clear now 🤗

  • @Nishchay-kc1gb
    @Nishchay-kc1gb 2 роки тому

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

  • @vigneshiyer1765
    @vigneshiyer1765 2 роки тому

    present mam!!

  • @surojitsantra7627
    @surojitsantra7627 2 роки тому

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

  • @galepraveen
    @galepraveen 2 роки тому

    samaj me aagaya didi, mereko samaj me aagaya

  • @jivanninawe3190
    @jivanninawe3190 2 роки тому

    Mam kitne question honge aur ismay

  • @manindhra748
    @manindhra748 2 роки тому

  • @varunendra.singh_
    @varunendra.singh_ 2 роки тому

    Day 24🙋‍♂️

  • @vamseekrishna3038
    @vamseekrishna3038 2 роки тому

    🖐♥

  • @nidhibansaliitr
    @nidhibansaliitr 2 роки тому

    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 2 роки тому

    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 2 роки тому

    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.