Write a program to print all permutations of a given string | GeeksforGeeks

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 131

  • @WimpyWarlord
    @WimpyWarlord 5 лет назад +59

    This is so robotic , there is no intuition , no insight , basically dry running !
    Partially helpful !

  • @WafflerMark
    @WafflerMark 5 лет назад +6

    This video demonstrates two things seldom seen in other explanations, and that makes this great! 1) The overlay of the translucent (low opacity) of the execution over the code, and 2) the inclusion of a detailed explanation of the execution. Well done; thank you!

  • @tushar3549
    @tushar3549 5 месяцев назад

    vector result;
    sort(S.begin(), S.end());
    do
    {
    result.push_back(S);
    } while (next_permutation(S.begin(), S.end()));
    return result;

  • @adeshshetty2830
    @adeshshetty2830 6 лет назад +21

    I think the i =2 case for Level 1 has a small mistake. Swapping A[0] and A[2] of ABC will give CBA. The video says CAB. Let me know if I am mistaken.

    • @rushikeshsatkar599
      @rushikeshsatkar599 3 года назад

      You are correct bro there is mistake in video but its ok cause this teacher explain it in best way to help us understand this concept easily , thank you

  • @ayushgupta7736
    @ayushgupta7736 3 года назад

    best ever explanation ever i watch for backtracking

  • @sander_bouwhuis
    @sander_bouwhuis 3 года назад

    1. This algorithm doesn't generate the permutations in sorted order, so I use a slightly different approach.
    2. What is with all the negativity in the comments? I for one think you explain it very exhaustively. Thumbs up!

  • @chinmaydas4053
    @chinmaydas4053 6 лет назад +10

    Very standard channel.. But don't know why very few subscribers.. people don't want to learn important difficult things.. want easy one..

  • @algotorials81
    @algotorials81 4 года назад +5

    you can have explained recursion by showing the allocation and execution of stack for a single permutation or any reference video of stack allocation or execution.

  • @ashishdukare1313
    @ashishdukare1313 4 года назад +13

    The audio is extremely bad. :(

  • @DebashisSahoo
    @DebashisSahoo 7 лет назад +3

    Asking a sincere question, Please don't mind.
    While calculating Time complexity of the algorithm, it's coming out to be N * N!
    I am clear with N!
    For N (That's taking to print the string each time) I am having doubt.
    Generally printing of a string takes constant time.
    Please justify.
    Thank you in advance. You have done a great job.

    • @prakharlondhe3876
      @prakharlondhe3876 7 лет назад +2

      it's not printing the string that is taking the time, it is the recursion which is going through the whole string, i.e N elements.

  • @TheAshishk007
    @TheAshishk007 8 лет назад +1

    +GeeksforGeeks I think the explanation is a little wrong. We are not backtracking from level 2 to level 1, as you mentioned at 2:18.
    You said we again "SWAP A and A" and come back to the original configuration, but backtracking is done while "SWAPPING B and B again" and " SWAPPING B and C again", after than no backtracking is done but "i" gets incremented and becomes 2 in For loop and A gets swapped with B which results in "BAC".
    Please check and explain me if i am understanding it wrongly.
    Thank you

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  8 лет назад +2

      +Ashish Kapil I see you're confusing the 'for-loop iteration where we move from left sub-tree of the root node to the middle sub-tree of the root node' with the 'backtracking step where we move from the level-1 of left sub-tree of the root node to the root node itself'. I hope this clears out your doubt, for a better understanding you might want to take a closer look at the dry run explained at 5:15.

  • @kaushaljhawar3282
    @kaushaljhawar3282 7 лет назад +10

    Nicely explained..
    please add interview question asked by the company during interview.

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +1

      Thank you, Kaushal. We are working on adding videos on programming interview questions.

  • @AbhishekYadav-ni5ts
    @AbhishekYadav-ni5ts 2 года назад

    Very well explained sirji .... Thank you sir

  • @pramodsrinivasan113
    @pramodsrinivasan113 8 лет назад +10

    I think there is a typo in the video :
    For i = 2,
    swap(A,C)
    permute(CBA,1,2) // instead of permute(CAB,1,2)
    swap(C,A)

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  8 лет назад

      +Pramod Srinivasan Thank you for pointing this out. You are right, it should be permute(CBA, 1, 2).

    • @jatinanand3726
      @jatinanand3726 5 лет назад

      Ek dum Correct bro i was noticing ...when i was dry running code😎☺😊

  • @theindiancookbook2268
    @theindiancookbook2268 7 лет назад +36

    can you please use a better mic from next time, audio volume is low and clarity is also not good.

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +5

      Thank you for the feedback.
      It is one of the very first videos we made, you will see that other videos on our channel have better audio quality. :)

  • @rushikeshsatkar599
    @rushikeshsatkar599 3 года назад +1

    Best Explanation ever, Thank you for shearing your knowledge with us...!

  • @johnk8174
    @johnk8174 3 года назад

    Best explanation of this (permutation) and backtracking. I see now that backtracking is analogous to a depth-first traversal of a tree.
    I still don't quite understand Heap's permutation algorithm, which only has one swap instead of two, and that swap is slightly different depending on the even-odd parity of the string; I would appreciate if someone could respond with a sort of comparison of this with Heap's algorithm for permutations, which is very similar.

  • @vivs005
    @vivs005 7 лет назад +1

    The way you've printed out Levels for each recursive call and dry run is very informative. Did you manually wrote this or used a piece of code to print out ? it would be very helpful in understanding recursion.

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +2

      Thank you, Amox.
      I wrote it down manually. But, I think using a piece of code to print the values of the variables at play is a fun thing to do.

  • @sayansanyal8259
    @sayansanyal8259 7 лет назад

    wow....i was searching for just this kind of stuff...thanks geeks for geeks...really quite a lucid explanation....actually none of the videos than i had gone through earlier this dry run and i was seeking for ythis only..really it was very useful

  • @dharmikbatra42
    @dharmikbatra42 3 года назад +1

    why its n*n! ? if we are printing n times then it should be n + n! which eventually be n! only??

  • @zaheerusmanutube
    @zaheerusmanutube 7 лет назад +12

    The very first loop is wrong, its supposed to be CBA not CAB, confused instead of explaining.

  • @niksgupta36
    @niksgupta36 5 лет назад +3

    In level 1, and i=2, it should be CBA instead of CAB ( as per the tree in the beginning).
    Let me know if my understanding is wrong.

    • @Aditya-us5gj
      @Aditya-us5gj 5 лет назад +1

      u r right. even i noticed it. we moved back to original string and then came up for swaping.

  • @vivekmaddeshiya
    @vivekmaddeshiya 2 года назад

    well explained,thankyou so much

  • @shafiaafrin1881
    @shafiaafrin1881 4 года назад

    Best explanation on function call . thak you.

  • @leenareddydesu3915
    @leenareddydesu3915 6 лет назад

    Excellent explaination sir

  • @vyshnavshabunair8358
    @vyshnavshabunair8358 4 года назад +2

    Thank you for the great explanation!

  • @heenahussain7044
    @heenahussain7044 7 лет назад

    Really like the solution and explanation. Thanks for this helpful video.

  • @dflag44
    @dflag44 2 года назад

    if string contains same characters repeated, it would give extra cases. so use hash map

  • @ssingh6768
    @ssingh6768 5 лет назад +1

    please see the subtitles around the 1:00 minute mark, it's strikingly wrong.

    • @uttammazumder7757
      @uttammazumder7757 5 лет назад

      The captions or subtitles are generated by the RUclips algorithm so it's nobody's fault

    • @uttammazumder7757
      @uttammazumder7757 5 лет назад

      It's also because of the perplexing accent of Indians!!

  • @manojdani8543
    @manojdani8543 7 лет назад +1

    I wrote the code with and without the backtracking step. Both programs produce all combinations correctly. The order is different. The program without backtracking created a sorted output vs. the one with backtracking did not create sorted output.(note the last two elements - CBA vs CAB) So I am failing to see the importance of backtracking? Is this really required here?
    Code for reference
    (without backtrack)
    for(int i=posn;i

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад

      Could you please share the complete code? I ran the code without backtracking (code.geeksforgeeks.org/645xpY) and got the following output:
      ABC
      ACB
      CAB
      CBA
      ABC
      ACB

    • @manojdani8543
      @manojdani8543 7 лет назад +1

      Here is the complete code (with backtrack)
      #include
      #include
      using namespace std;
      void permute(string input, int posn)
      {
      if(posn >= input.size())
      {
      cout

    • @aswinbeats
      @aswinbeats 7 лет назад +1

      It depends on whether you're creating a new string in your swap method or not. If you work on the same character array, you have to backtrack.

  • @jitendrabaug6928
    @jitendrabaug6928 8 лет назад +1

    please continue uploading videos. This was very nice..

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  8 лет назад

      +Jitendra Baug Thank you!
      We are working on adding more videos. Stay tuned.

  • @Abhay-if9cb
    @Abhay-if9cb 5 лет назад

    background noise is so annoying,but thanks for making the video it helped me alot

  • @chandrashekhar9470
    @chandrashekhar9470 4 года назад

    Regarding time complexity.I had a doubt , How can printing the string became O(N).it should be O(1).Please clarify!

  • @kudorgyozo
    @kudorgyozo 5 лет назад +3

    great, next time do it with sound

  • @shriharikulkarni8229
    @shriharikulkarni8229 5 лет назад

    Good explanation sir

  • @VC-kj9yx
    @VC-kj9yx 8 лет назад +3

    very nice videos can you upload solutions of programming questions asked in amazon,google etc?

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +1

      Thank you, Vidhit. We are working on adding videos for programming interview questions.

  • @DragonStoneCreations
    @DragonStoneCreations 6 лет назад +4

    Please remove the background noise , It's too Anoying ! anyways good job .

  • @ShivamSajwan
    @ShivamSajwan 6 лет назад +5

    how to eliminate duplicate combinations?

  • @kunjanshah_sr
    @kunjanshah_sr 4 года назад

    public class Permutations extends Activity {
    String TAG = Permutations.class.getSimpleName();
    char[] Res;
    int[] A;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String input="ABC";
    Res = new char[input.length()];
    A = new int[input.length()];
    perm("ABC".toCharArray(), 0);
    }
    void perm(char[] str, int k) {
    if (str.length == k) {
    Log.d(TAG, Arrays.toString(Res));
    } else {
    for (int i = 0; i

  • @dushyantkumar7534
    @dushyantkumar7534 6 лет назад

    sir can you tell me which type of program i have to prepare for MAQ Software test,,,your all videos are help me???

  • @NaveenGaming2001
    @NaveenGaming2001 3 года назад +1

    To much noise

  • @thepunisheeer7546
    @thepunisheeer7546 7 лет назад +2

    i cant understand that recursive part (permute)..cud u plz explain me sir,,i dunno how the value of l retains at 0 itself...need answers plz

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +3

      When we call the permute function for the first time we pass two values: 0 and n-1. Inside the permute function, 0 is then assigned to L and n-1 is then assigned to R. L represents the starting index of the string to be permuted and R represents the ending index of the string to be permuted.
      First of all, inside permute, we check whether L==R to see if there's any thing left to permute. If L==R, we know that we have reached a Leaf Node of the Recursive Tree that was shown in the beginning. If L!=R, we know that we can there more than one characters to be permuted. So, we run a loop from L(eft) to R(ight) and at each step, we swap the element at position L with the element at the ith position.This modifies our string by the interchanging the positions of the character at Lth position and the character at ith position.
      However, in the first iteration of the loop, we notice that L=0 and i=0. So, the string has not yet been modified. But, we have come to one level below the root node of our Recursion tree. I request you to draw the recursion tree and see how the code takes you through the various nodes of the recursion tree.
      Feel free to ask more questions. :)

  • @KirubaEbenezer
    @KirubaEbenezer 8 лет назад

    Audio quality worst.. I didn't expect this from g2g.... Come on guys u ppl worth a lot.. Don't let these small things to spoil ur work....

  • @JanacMeena
    @JanacMeena 5 лет назад

    For those of you complaining about the video - here is an alternative: ruclips.net/video/78t_yHuGg-0/видео.html

  • @vinayvinni7804
    @vinayvinni7804 6 лет назад +1

    plz do make practical approach clear.it's confusing..

  • @sheetalshireen
    @sheetalshireen 8 лет назад

    Will it provide correct results in case of characters repetition in given string?

    • @TheAshishk007
      @TheAshishk007 8 лет назад +1

      +Sheetal No it won't , answers will be in repitition

  • @ivandrofly
    @ivandrofly 3 года назад

    I think the second ` str = Swap(str, l, i);` is not requied...

  • @harshit438
    @harshit438 7 лет назад

    At level 1, how is it possible for the loop to run for i=0.We have given the initial value value of i=1. So the actual output should be
    OUTPUT
    BAC
    BCA
    CBA
    CAB

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +1

      I see you're confusing i=l (L in small alphabets) with i=1 due to the font used in the code.
      The loop is running from i=l (signifying left) to i

    • @harshit438
      @harshit438 7 лет назад

      Thanks

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад

      You're welcome, Harshit!

  • @mahmudulislam8593
    @mahmudulislam8593 5 лет назад +1

    Can you please use a better mic and can speak clearly then it will help us a lot .TIA

  • @saurabhverma3453
    @saurabhverma3453 4 года назад

    why we have swap for backtracking. Can you plz check without this swap. I think it'll work without this second swap also.

  • @MadhuriDixitannavazzala
    @MadhuriDixitannavazzala 7 лет назад

    Awesome.....keep up the gud work guys..

  • @codestorywithMIK
    @codestorywithMIK 7 лет назад

    very helpful...

  • @anurag4316
    @anurag4316 4 года назад

    i want to store the value of permutation then print it

  • @hk_build
    @hk_build 6 лет назад

    can you bit slow so that we can understand it better.

  • @mayanktripathi3915
    @mayanktripathi3915 5 лет назад

    Could we do it using bfs or dfs

  • @allanjasonmburu2186
    @allanjasonmburu2186 6 лет назад

    have you done this in java?

  • @onkarsingh4823
    @onkarsingh4823 7 лет назад

    recursive calls to permute function again and again is very confusing. try to explain step by step

  • @fahimtajwarsaikat4363
    @fahimtajwarsaikat4363 5 лет назад

    i think the solution is wrong. Think if there are any repetition.

  • @dipayanray2847
    @dipayanray2847 4 года назад +1

    If i give input : AAB
    Output Should be: AAB ABA BAA
    BUT Your output: AAB ABA AAB ABA BAA BAA

    • @LNMLucasMasiero
      @LNMLucasMasiero 4 года назад

      and it should be like that. Cuz the order is important. which means with a 3 letter's word, there should be 6 permutations

  • @casual_chess
    @casual_chess 6 лет назад

    Well done sir

  • @karrymighati4770
    @karrymighati4770 7 лет назад +2

    how can we find all possible palindromic partitions of given string?
    nitin
    n i t i n
    n iti n
    nitin?

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +1

      Following resource might help you: www.geeksforgeeks.org/given-a-string-print-all-possible-palindromic-partition/

    • @ShinAkuma
      @ShinAkuma 7 лет назад

      Do you want Palindrome substrings from a original Palindrome string or from a original non-palindrome string.
      If original string is palindrome each substring from i+k to length-k will be a palindrome where K varies from 1 to length/2.

  • @tanmaysinghal8370
    @tanmaysinghal8370 3 года назад

    can someone please tell me the use of the second swap line, as I'm getting the same output without adding 2nd swap too

    • @saitejavaddepalli3400
      @saitejavaddepalli3400 3 года назад +1

      That do work in java or python because every time a new space is created for the string in every recursion, but in C/C++ we are directly swapping those in the same address block of the memory which affects the order, so we need to swap it again to have the same order.

  • @dracomalfoy1245
    @dracomalfoy1245 5 лет назад

    could have used a different font... cant differentiate 1 and L .....

  • @keshabdas3065
    @keshabdas3065 6 лет назад

    Sir is this series enough for India Computing Olympiad?

  • @SourovDattacse
    @SourovDattacse 5 лет назад

    Thanks for your effort. Its fine. However, Mr tutorial maker, Your voice speed so robotically. You did not properly articulate the lecture. Otherwise everything is good.

  • @duaasaleem539
    @duaasaleem539 5 лет назад

    Can we do this without function

  • @DragonStoneCreations
    @DragonStoneCreations 6 лет назад

    Please remove the background noise , It's too Anoying ! anywise good job .

  • @yuvrajsinghtanwar402
    @yuvrajsinghtanwar402 7 лет назад

    great work

  • @meenas2754
    @meenas2754 7 лет назад

    very nice thank u so much

  • @darshantsdarshan1
    @darshantsdarshan1 6 лет назад

    Thanks for the video :)

  • @arnavkarforma3015
    @arnavkarforma3015 5 лет назад

    But most importantly it won't work for repeated characters....its simply showing values for n! times

    • @mdlwlmdd2dwd30
      @mdlwlmdd2dwd30 3 года назад

      The problem did not specify duplicated values or not. You can easily get rid of duplicated value by if(i>start&&arr[i]==arr[i-1])continue; after sorting original array

  • @ahmedsohail9855
    @ahmedsohail9855 6 лет назад

    helo,how does your wriiten code work please explain
    #include
    using namespace std;
    void fun2(int n);
    int main()
    {
    int n=100;
    fun2(n);
    return 0;
    }
    void fun2(int n)
    {
    int LIMIT=1000;
    if (n LIMIT)
    return;
    cout

  • @ash_engineering
    @ash_engineering 8 лет назад

    is there any better approach to solve this question...

    • @YaskSrivastava
      @YaskSrivastava 8 лет назад +1

      Yes, you can use prefix-remaining approach:
      github.com/yask123/interview_prep_python/blob/master/permutate.py

  • @jacobstech1777
    @jacobstech1777 5 лет назад

    best video explain this!

  • @Pariharyash
    @Pariharyash 7 лет назад

    thanks

  • @marcinkovalevskij5820
    @marcinkovalevskij5820 6 лет назад

    Thanks you. But the MIC!

  • @ranjay4473
    @ranjay4473 4 года назад

    That's a really poor explanation....but thumbs up for the code snip

  • @intuit13
    @intuit13 7 лет назад +1

    speed 0.75

  • @tapanjeetroy8266
    @tapanjeetroy8266 6 лет назад

    If you can not explain then don't try to do so

  • @MijanurRahman-jo1st
    @MijanurRahman-jo1st 6 лет назад

    I think this tutorial is not so good, try for individual string.

  • @jg8742
    @jg8742 5 лет назад +3

    this person is simply running . very poor explanation

  • @jsonprry3889
    @jsonprry3889 7 лет назад

    为什么这种视频全是印度口音

  • @kaviyarasanm67
    @kaviyarasanm67 5 лет назад +2

    deii oru mayirum puriyala da thelivaa sollu da

  • @achyuthsai6339
    @achyuthsai6339 4 года назад

    her i = 0

  • @riteshkumar-br6jq
    @riteshkumar-br6jq 5 лет назад

    it seems that you are in hurry to make this video. dislike for your explaination!!!!!!!

  • @sanjananaidu7966
    @sanjananaidu7966 5 лет назад

    worst xplanation

  • @ahmedsohail9855
    @ahmedsohail9855 6 лет назад

    helo,how does your wriiten code work please explain
    #include
    using namespace std;
    void fun2(int n);
    int main()
    {
    int n=100;
    fun2(n);
    return 0;
    }
    void fun2(int n)
    {
    int LIMIT=1000;
    if (n LIMIT)
    return;
    cout