L13. Print all Permutations of a String/Array | Recursion | Approach - 2

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • Check our Website:
    In case you are thinking to buy courses, please check below:
    Link to get 20% additional Discount at Coding Ninjas: bit.ly/3wE5aHx
    Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
    Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
    Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    ---------------------------------------------------------------------------------------------------------------------------------------------------- Please watch this video before watching this: • L12. Print all Permuta...
    I have decided to make a free placement series comprising of video lectures(C++ and Java) on the entire SDE sheet.. ✅(bit.ly/takeUforward_SDE)
    ✅Entire Series: bit.ly/placementSeries
    ✅Problem Link: leetcode.com/problems/permuta...
    C++/Java Code: takeuforward.org/data-structu...
    ✅This is where I teach: ----- Use coupon-code "TAKEUFORWARD" for getting 10% on my course at GFG: bit.ly/striver_cpCourse
    ✅Use coupon-code "TAKEUFORWARD" for getting 10% for all GFG courses: bit.ly/tuf_gfgCourse
    If you appreciate the channel's work, you can join the family: bit.ly/joinFamily
    Thumbnail Creator: / rikonakhuli
    ✅ Striver's Linkedin Profile: / rajarvp
    ✅ Instagram: / striver_79
    Connect with us: t.me/Competitive_Programming_tuf (Use Link in Mobile only, if not works search "takeUforward" in telegram)..
    #dsa #leetcode #placements

Komentáře • 257

  • @takeUforward
    @takeUforward  Před 3 lety +64

    Please leave a small comment if you understand by spending your time here, every comment motivates me more :)
    C++ Code: github.com/striver79/SDESheet/blob/main/permutationsCppApproach-2
    Java Code: github.com/striver79/SDESheet/blob/main/permutationsJavaApproach-2
    Approach-1 Video Link: czcams.com/video/YK78FU5Ffjw/video.html
    Instagram(For live sessions): instagram.com/striver_79/

    • @ghj7275
      @ghj7275 Před 2 lety

      Can you please check the below code for Permutation - II problem on leetcode, It fails for [1,2,2,3,3].
      class Solution {
      List res = new ArrayList();
      public List permuteUnique(int[] nums) {
      Arrays.sort(nums);
      f(0, nums, nums.length);
      return res;
      }
      public void f(int ind, int nums[], int n) {
      if(ind == n-1) {
      List li = new ArrayList();
      for(int i : nums)
      li.add(i);
      res.add(li);
      return;
      }
      for(int i = ind; i < n; i++) {
      if(i != ind && nums[i] == nums[i-1])
      continue;
      swap(nums, ind, i);
      f(ind+1, nums, n);
      swap(nums, ind, i);
      }
      }
      public void swap(int nums[], int i, int j) {
      int t = nums[i];
      nums[i] = nums[j];
      nums[j] = t;
      }
      }

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

      @@ghj7275(i!=ind && nums[i]==nums[i-1] ) works only when array is sorted.here swapping distorts the sorted array so this is not gonna work.

  • @mohammedwaseem8599
    @mohammedwaseem8599 Před 3 lety +155

    Hello bhaiya i hope you are doing extremely well

  • @subhadipmaity3253
    @subhadipmaity3253 Před rokem +84

    Sir I think you have no need to say "Please please like this video, and subscribe this channel", those who really understands coding will definitely give value and respect to you.
    Thank You so much Sir for all the amazing series.

  • @Anonymous-uj3jx
    @Anonymous-uj3jx Před 2 lety +90

    A year back when I saw this question on gfg, I spent nearly 5 hours understanding this solution, and now after seeing your approach I could code it myself, you are a legend man. And you are giving all this for free, I pray to God that you will be blessed with good health and prosperity.

    • @pcgaming6597
      @pcgaming6597 Před 27 dny +2

      Thank you for this comment cause I though, for me only it's taking 5 hours to understand a question. I think I am wrong now 🤗

  • @abhi007rider
    @abhi007rider Před 2 lety +50

    Completed the whole Tree,Graph and Recursion series. Striver you are a gem

  • @tanmaysinghal8370
    @tanmaysinghal8370 Před 2 lety +23

    damn i just love this guy, he explains full recurssion tree till end.... HATS OFF TO YOU BROTHER

  • @zeeshanequbal6227
    @zeeshanequbal6227 Před 2 lety +19

    BONUS: This method can also be used to solve Permutation II problem of Leetcode, where duplicate elements could be present in the array. The only thing to take care in this case is that we are dependent on the sorted order of Array to check for and skip duplicates, so either don't pass the array as reference or restore the original array before exiting. Here is the C++ solution for Permutations-II using this approach.
    class Solution {
    public:
    // Dont pass nums by refernce, cause that will mess up our sorted order.
    // Or, if passing by reference, then swap ALL positions back so that array remains sorted.
    void solve(vector &nums, int idx, vector &res) {
    // Base Case
    int n = nums.size();
    if(idx == n) {
    res.push_back(nums);
    return;
    }
    // Recursive Step
    for(int i = idx; i < n; i++) {
    if(i != idx && nums[i] == nums[idx]) continue;
    swap(nums[i], nums[idx]);
    solve(nums, idx + 1, res);
    }
    for(int i = n - 1; i > idx; i--) // Dont need to do this if passing nums by value.
    swap(nums[i], nums[idx]);
    }
    vector permuteUnique(vector& nums) {
    vector res;
    sort(nums.begin(), nums.end());
    solve(nums, 0, res);
    return res;
    }
    };

    • @nitwaalebhaiya
      @nitwaalebhaiya Před 2 lety +6

      for(int i = n - 1; i > idx; i--) // Dont need to do this if passing nums by value.
      swap(nums[i], nums[idx]);
      Thanks. This is awesome.

    • @parissweetheart96
      @parissweetheart96 Před 2 lety

      @@nitwaalebhaiya Not passing leetcode test case. What is wrong in this. Here is my java code:
      class Solution {
      private void recurPermute(int index, int[] nums, List ans) {
      if(index == nums.length) {
      // copy the ds to ans
      List ds = new ArrayList();
      for(int i = 0;i

    • @Rajat_maurya
      @Rajat_maurya Před 2 lety

      @@parissweetheart96 class Solution {
      public:
      void f(vector &v1,vector &v2,int i)
      {
      if(i==v1.size())
      {
      v2.push_back(v1);
      return ;
      }
      for(int j=i;ji) && v1[j]==v1[j-1]) continue;
      swap(v1[j],v1[i]);
      f(v1,v2,i+1);
      swap(v1[j],v1[i]);
      }

      }



      public:
      vector permuteUnique(vector& v1) {
      vector v2;
      sort(v1.begin(),v1.end());
      f(v1,v2,0);
      return v2;
      }
      };
      can you please help me whats wrong in this solution for permutation II

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

      @zeeshan Can you please explain the reverse swapping I'm not understanding that part

  • @vishalgowrav
    @vishalgowrav Před 2 lety +58

    I solved this question by my own using this approach just by watching first 6 minutes of video,Felt very happy afterwards. Your explainations and approaches are top notch.Thank u for this series Striver

  • @aakashyadav6228
    @aakashyadav6228 Před 3 lety +34

    Bro please complete the placement series before the placement season. I can guarantee you that this series will be the most watched series in the coding community in next few years. I know you have your health issues and job as well. But please help us all out whenever free. Kudos to you and keep up the good work !

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

    Best Approach. Can't find more simpler than this !! Thank you Striver

  • @Hari-mm9eo
    @Hari-mm9eo Před 2 lety

    You are explaining it more clear than others, cheers mate!

  • @aryansinha1818
    @aryansinha1818 Před rokem

    You explain things at a different level, which is very easy to understand. Thank you, sir. Love and respect always.

  • @adityamahimkar6138
    @adityamahimkar6138 Před 2 lety +23

    The moment I heard swapping, I completed the code while only half video was over. I'm showing growth, thanks to you bro :) ✨

    • @AbhishekYadav-ni5ts
      @AbhishekYadav-ni5ts Před 2 lety

      Brother what does vector means in line 16 ..... Does it means that the return type will be vector... Please explain to me..

    • @AbhishekYadav-ni5ts
      @AbhishekYadav-ni5ts Před 2 lety +1

      Is it returning multidimensional vector...?? I am confused please help

    • @adityamahimkar6138
      @adityamahimkar6138 Před 2 lety +4

      @@AbhishekYadav-ni5ts yes it is a nested vector or multidimensional vector

    • @AbhishekYadav-ni5ts
      @AbhishekYadav-ni5ts Před 2 lety +1

      @@adityamahimkar6138 ohh... Thank u bhai 🙏

    • @vishalgowrav
      @vishalgowrav Před 2 lety

      Even i did the same bro!

  • @adityasankhla1433
    @adityasankhla1433 Před 10 měsíci

    Holy Moly. Solved the question after watching just the first 2 mins of the video. Crystal clear explanation and intuition building striver!

  • @Manishgupta200
    @Manishgupta200 Před rokem +1

    Amazing Striver ❤ I'm preety much unsure about TC and SC but now clear after watching your tutorial

  • @Believer...
    @Believer... Před 10 měsíci +2

    i am speechless bro!! no one on youtube want to put that much hard efforts to make us understood. you clear the concept level by level which impact our problem solving tremendously.

  • @AYUSHKUMAR-xj4wc
    @AYUSHKUMAR-xj4wc Před 10 měsíci

    This is the cleanest approach to solve this problem. I would prefer this one. Thanks Striver❤❤❤❤

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

    guruji maan gaye apko! you explained each line i watched 3 video's and i was confused how the i is moving to its next place before recursive call and you just made it clear .....thanks man!

  • @ajinkyadeshpande3992
    @ajinkyadeshpande3992 Před 2 lety

    Such a clean solution. Perfection.

  • @athangkulkarni8245
    @athangkulkarni8245 Před 7 měsíci

    Striver, you are an absolute jod!
    Watched initial explanation and tried to code on my own and coding never felt this easy!
    Thanks man

  • @ahrazmalik5807
    @ahrazmalik5807 Před 3 lety +1

    One of the best teacher on youtube❤️

  • @sankalpsharma4890
    @sankalpsharma4890 Před 3 lety +3

    I was trying this question from long time. Thank you so much...

  • @kratika_agrawal28
    @kratika_agrawal28 Před 3 lety +2

    You are videos are really helping me .
    Keep making videos like this

  • @rudra_anand
    @rudra_anand Před rokem

    Your teaching skills are exceptional.

  • @sandeepnallala48
    @sandeepnallala48 Před 3 lety +3

    raj bro u are really master of Recursion. the way you draw the Rec tree make things easy to understand brother.

  • @saketjaiswal9381
    @saketjaiswal9381 Před rokem

    2 years old video still ( striver u r literally god brother thank u from the bottom of my heart will meet u surely )😀😀

  • @thinker-o5p
    @thinker-o5p Před 2 lety

    god level teaching you cleared all my doubts in one shot

  • @sumeetubale3923
    @sumeetubale3923 Před rokem +2

    A Big Thank You to Striver Bhaiya for this Amazing series !!!❤❤❤

  • @studynewthings1727
    @studynewthings1727 Před 9 měsíci

    Thankyou so much STRIVER, I have understood all the code very well. I able to draw all the function calls made by the function again and again.

  • @techwithrajdeep7633
    @techwithrajdeep7633 Před 2 lety

    Op explanation!!!! When understood the intuition it was really great.

  • @govindmajage2317
    @govindmajage2317 Před 3 lety +6

    Perfect recursion series tysm bhaiya👍🙏

  • @gandhijainamgunvantkumar6783

    Thank you so much bhaiya for such a crystal clear and amazing explanation :)

  • @awanishtiwari8683
    @awanishtiwari8683 Před rokem

    I have much fear 😨 that how does these recursive calls flow but with the cute explanation from your videos I got to know it very efficiently
    Thanks alot for your efforts

  • @rahulgovindkumar3105
    @rahulgovindkumar3105 Před 3 lety +2

    You are videos are really really really helping me .
    Plse......Keep making videos like this

  • @notdumb3182
    @notdumb3182 Před rokem +2

    Hey, I just want to let you know if I make it to good company you will be the main reason behind it. This videos are real gem hope they stay free here on CZcams.
    Wish you a very good life ahead

  • @siddharthkhandelwal933
    @siddharthkhandelwal933 Před 11 měsíci +9

    Thanks for all of your videos. The explanations are great.

  • @shastriamisha
    @shastriamisha Před 2 lety +6

    Amazing explanations, loving the recursion playlist, cant wait to start DP :) Thank you very much for this amazing content.

  • @anmoljoshi8275
    @anmoljoshi8275 Před 2 lety

    Great explanation. Thanks!!

  • @mohitagarwal1888
    @mohitagarwal1888 Před 2 lety

    This was better in terms of consuming less space as well as easier understanding.

  • @sonusah6620
    @sonusah6620 Před rokem

    No words to express.....GOAT for a reason

  • @chincharm
    @chincharm Před 7 měsíci +1

    Thanks for this video man!!! Keeping going forward❤

  • @geekyaman2297
    @geekyaman2297 Před 3 lety +6

    You are none other than god/ best guru for any tier 3 student!
    Jitna thanx kru kam h bhai apke liye🙏❤❤❤❤

  • @pranavm002
    @pranavm002 Před 2 lety

    This is very smart solution...probably a solution that will make you stand out!,

  • @stith_pragya
    @stith_pragya Před 5 měsíci +1

    UNDERSTOOD......Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @anujmishra778
    @anujmishra778 Před rokem

    raj bhai i really enjoying learning your this series , aap ka video dekh kar lagta hai aap har ek topic ko samjhane me bahot mehnat karte ho bhai, thanks a lot bhai

  • @neyovyas3992
    @neyovyas3992 Před 3 lety

    Nice explanation understood both the approach

  • @bmishra98
    @bmishra98 Před 9 dny

    Base case can also be if(index == candidates.size() - 1) {...}, as when 'index' reaches 'candidates.size() - 1', no change in position of elements occur on further recursive call. Thus, the 2nd last level of recursion can also be treated as the base case.

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

    thanku sir , i solved the whole question by myself just after listening the hint of swapping technique.

  • @techcode356
    @techcode356 Před rokem

    Thank YOU !!! It was really helpful

  • @abhijeetbasfore6816
    @abhijeetbasfore6816 Před 2 lety

    I solved this question without watching this video. Awesome tutorials

  • @sumitSharma-ed5mi
    @sumitSharma-ed5mi Před 2 lety

    very helpful and easy to understand.

  • @lomeshdaheria9960
    @lomeshdaheria9960 Před 3 lety +3

    Thank you bhai for your great efforts😁🙏❤️

  • @aanchalmittal9897
    @aanchalmittal9897 Před 2 lety

    Simply Amazing!!!

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

    funny thing was that for me it was the first idea i had before the brute force approach😅

  • @awanishtiwari8683
    @awanishtiwari8683 Před rokem

    I hope you are doing extremely well 😀😀😌😌

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

    why is it rec(index+1) and not rec(i+1), in both combination sum 2 and subset 2 the recursion was rec(i+1) similar approach, confused here...

  • @gunratnamore4250
    @gunratnamore4250 Před 2 lety

    thanks striver !!! all doubts cleared :).

  • @ShubhamPatidar16
    @ShubhamPatidar16 Před 3 lety +1

    Thank u so much bhaiya and please bring backtracking problems

  • @prathamsagar5353
    @prathamsagar5353 Před rokem +1

    I like ur style of explaining and it all goes in my head. BUT I am afraid if I will be able to figure all this out in an interview???

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

    Thanks God, for making Striver

  • @chidambarjoshi3470
    @chidambarjoshi3470 Před rokem

    I have tried another approch using emplace back,
    vector ans;
    sort(nums.begin(), nums.end());
    do {
    ans.emplace_back(nums);
    }while(next_permutation(nums.begin(),nums.end()));
    return ans;

  • @shubham7668
    @shubham7668 Před rokem +1

    Base condition could be (index=nums.size()-1)
    Coz basically we are doing nothing in last swap

  • @dhanashreegodase4445
    @dhanashreegodase4445 Před 2 lety

    Amazing explanations

  • @rohanraonallani561
    @rohanraonallani561 Před 3 lety +3

    Also,try to discuss the heap's algorithm if possible

  • @ShivamGupta-cx3hy
    @ShivamGupta-cx3hy Před 3 lety +1

    Thank You so Much

  • @Yogamindmastery
    @Yogamindmastery Před 3 dny

    tu es un genie

  • @sarthak_dms
    @sarthak_dms Před 2 lety

    man this trick was amazing

  • @vanshajgupta4635
    @vanshajgupta4635 Před 6 měsíci

    Amazing man!!

  • @akshitsingh2598
    @akshitsingh2598 Před 10 měsíci +1

    why do we need to wait for the pointer to go out , we are getting all permutations when pointer is at n-1 , so that could be our base condition.

  • @neyovyas3992
    @neyovyas3992 Před 3 lety +3

    Please try to upload 1 video daily it will be so helpful

  • @dhanashreegodase4445
    @dhanashreegodase4445 Před 3 lety

    Thank you so much...

  • @chandrachurmukherjeejucse5816

    Great lecture 🔥

  • @popli10
    @popli10 Před 9 měsíci

    best explanation love you bhaiya

  • @kumarivandana1554
    @kumarivandana1554 Před 2 lety

    Great explanation

  • @Sumeet_100
    @Sumeet_100 Před 11 měsíci

    Thank You !!

  • @SuperWhatusername
    @SuperWhatusername Před 2 lety

    Thank you Striver

  • @rishikumar218
    @rishikumar218 Před 11 měsíci +2

    This Approach is only work when there is not matters of orders of output(print any order) but if need to print lexographical order it may fails even after sort the array initialy. correct if i'm wrong??

  • @TokyoWasTaken
    @TokyoWasTaken Před rokem

    bro you life saver

  • @arshdeep011
    @arshdeep011 Před rokem

    Bro you are the best 😍🥰🥰🥰

  • @user-sn8wm6su5c
    @user-sn8wm6su5c Před 10 měsíci

    Hats off!!!

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

    Most underrated channel 😞😞😞

  • @JyotiprakashMandal-bp8ng

    great explanation

  • @saisubhank8011
    @saisubhank8011 Před 3 měsíci +1

    I dint understand why we need to reswap? It would be great if you can explain.

  • @San-ix7ki
    @San-ix7ki Před rokem

    studied recursion from many resources but was not able to get how to write code from tree there I was able to make tree from code but after striver's series I am able to do that finally...hats off to this man
    thanks alot..

  • @vishalplayzz2580
    @vishalplayzz2580 Před rokem

    too good man ❤

  • @coefficient1359
    @coefficient1359 Před 3 lety +4

    Understood 👍

  • @Ram-vg5fu
    @Ram-vg5fu Před 3 lety +2

    Loved ur videos.. when will u resume doing sde sheet pls reply bro 🙂

  • @laxminarayanchoudhary939

    thank you

  • @yuvrajluhach5665
    @yuvrajluhach5665 Před 2 lety

    Moving to L14👍

  • @devinpadron5
    @devinpadron5 Před 5 měsíci +1

    Time and space complexity 13:34

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

    What change to existing code will give us unique permutaions??

  • @Sarkar.editsz
    @Sarkar.editsz Před rokem

    thanks a lot sir

  • @gaurishankarchille9935

    Best explaination

  • @pragatipandey3478
    @pragatipandey3478 Před 2 lety

    very helpful

  • @UECAshutoshKumar
    @UECAshutoshKumar Před rokem +1

    Thank you sir

  • @user-cl9cn5xu4e
    @user-cl9cn5xu4e Před 4 měsíci

    Great great great 😊

  • @husler7424
    @husler7424 Před 2 lety

    Is the Swapping approach only for this problem only or it can be used for another problem too? Please someone reply.

  • @shivrajpavaskar5360
    @shivrajpavaskar5360 Před rokem

    Thanks bro

  • @abbasnaqvi5381
    @abbasnaqvi5381 Před rokem

    what if we change the value of nums array to 11 just to mark it as visited and unmark it when we comeback?
    will this help in removing the extra bool array space?

  • @amansamal8233
    @amansamal8233 Před 2 lety

    understooood😍😍