How To Permute A String - Generate All Permutations Of A String

  Рет қаралды 110,629

Back To Back SWE

Back To Back SWE

Күн бұрын

Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe....
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: Given a string, print all permutations of the string and return an array of them. No duplicates are allowed.
Whenever we work with problems like this, the fact that it is a string and that it is an array are interchangeable. We will permute an array the same way that we permute a string.
Why is this a backtracking problem? Because we are placing an item and then exploring all possibilities from there.
Whenever we have a problem that says "generate" or "compute" and it is an expression of several decision points that comprise a larger possibility set...WE HAVE BACKTRACKING.
The 3 Keys To Backtracking
Our Choice
What character we place in a "slot"
Our Constraints
None really...but at each decision point we will have fewer characters to work with because of our previous decisions.
Our Goal
Let n be the string length. Place n characters.
Complexities
Time: O(n * n!)
There are n! permutations and it takes O(n) time to add each one to our result array
Space: O(n)
We are not returning an array here so linear space because our recursion will go at maximum n elements deep since we make n choices of placement at maximum
If we did store and return an array our space complexity would be O(n * n!) since we would have n! permutations and each permutation would be of length n. If we consider the returned array of all permutation strings as NOT part of space, the call stack dominates space. We are back to O(n).
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: / @hackerrankofficial
Tuschar Roy: / tusharroy2525
GeeksForGeeks: / @geeksforgeeksvideos
Jarvis Johnson: / vsympathyv
Success In Tech: / @successintech
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is number 16.3 in the fantastic book "Elements of Programming Interviews" by Adnan Aziz

Пікірлер: 248
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
Table of Contents: (I'm sorry, I messed up the audio and am talking pretty loud) Problem Introduction 0:00 - 1:57 The "3 Keys To Backtracking" 1:57 - 4:55 The FULL Recursion Walkthrough For "boat" 4:55 - 22:57 Time & Space Complexities 22:57 - 27:53 Wrap Up / Conclusion 27:53 - 28:18 This is super long because of the walkthrough...I'm sorry. Here is a good example of the code: javarevisited.blogspot.com/2015/08/how-to-find-all-permutations-of-string-java-example.html (If this link doesn't work then you can find the code anywhere, just know the concepts and you can implement it however you want, but I really should have walked through that code snippet. I will try to this in a later video, this is my fault.)
@jaseharold3119
@jaseharold3119 3 жыл бұрын
pro trick: you can watch series at flixzone. Me and my gf have been using them for watching lots of of movies lately.
@samirmanuel8944
@samirmanuel8944 3 жыл бұрын
@Jase Harold Yea, been using flixzone} for since december myself =)
@optimizedpran1247
@optimizedpran1247 4 жыл бұрын
I want to get "No More Decisions To Make Backtrack" tattooed on my arm after this.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
hahaha same
@akn4336
@akn4336 5 жыл бұрын
Write a code to count number of “now”s in this video! 😂 Just kidding, good tutorial
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
I say that a lot. Weird mannerism. I can't control them....well I could consciously try...eh...whatever
@AlexBonillas
@AlexBonillas 4 жыл бұрын
Time complexity for that proposed "and now"-count Algorithm is the same as this permutations Algorithm.... also kidding of course, this is a good tutorial
@ElGalloUltimo
@ElGalloUltimo 5 жыл бұрын
11:51 "I feel like a robot right now" :) But you are not a robot, so it is exhausting for a human being to do this. It's amazing how much simpler the actual code is than doing it manually yourself. It is one of the most powerful examples of why computers are useful. Despite all that, I *really* appreciate you putting yourself through that on video because it fully and completely demonstrates the intuition behind the code.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
sure thx
@yangyu4489
@yangyu4489 4 жыл бұрын
love the ENERGY my guy - earned a sub
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thx
@vishalcelestine8654
@vishalcelestine8654 4 жыл бұрын
Not the teacher we deserve, but the teacher we need..hats off !!!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks haha
@LeafNBlade
@LeafNBlade 5 жыл бұрын
Amazing explanation, I have a interview coming up and i will keep this in mind for any permutation problem!
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
love
@faisalsal1
@faisalsal1 5 жыл бұрын
Jeez, I thought he is going to write a code :(
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
Here Is My Response: backtobackswe.com/im-sorry Github: github.com/bephrem1/backtobackswe/issues/1
@NB19273
@NB19273 5 жыл бұрын
Amazing explanation very clear! I did a masters (1 year conversion course) in Computer Science and they never even mentioned the concept of backtracking so this is very helpful thanks.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
sure
@sushmithasnataraj5122
@sushmithasnataraj5122 4 жыл бұрын
Who else feels that this channel deserves more subscribers?
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
It does alright. The growth rate is good. It can be faster and we haven't gone on to create "viral" content. Since November I've been mostly coding and not focused on growing the channel. Between December 2018 and November 2019 I only made niche programming question videos and they operate like a library. A library is stable and grows slowly. The channel still operates like a library and not an active mechanism of rich media generation. If we made media that could scale to capture more people (less technical, talk about salaries or something which is a hot, attractive topic) we would grow exponentially. I am well aware where we stand and what the KZbin is as well as where it can go. Entities on the internet can deserve things, but attention distribution is not fair and follows the pattern of natural selection with a great bit of unfairness.
@sushmithasnataraj5122
@sushmithasnataraj5122 4 жыл бұрын
@@BackToBackSWE I love the content on this channel. Do you know what makes this channel different from the rest? You guide us through the approach and you don't explain the code. After watching the video I can code in like 5 minutes and I will never forget the solution. Thank you so much for all the videos and keep making quality voices. 🙂
@debdutsaha4316
@debdutsaha4316 3 жыл бұрын
Also I think so @Sushmita S Nataraj
@jasasul8164
@jasasul8164 4 жыл бұрын
I've never had so much fun watching an educational video, the backtracking steps explanation is so great Love your attitude, your persnoality and you explain things so well Great Video
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thx
@PatteeGreen
@PatteeGreen Жыл бұрын
You're addictive to watch, incredibly entertaining, and amazingly informative. Thanks!!
@BackToBackSWE
@BackToBackSWE Жыл бұрын
thank you. means a lot. Would love some feedback on the free 5 day course - backtobackswe.com/
@trestenpool9045
@trestenpool9045 3 жыл бұрын
The code was tricky to wrap my head around. This helped very much, thank you!
@milkthecode7369
@milkthecode7369 4 жыл бұрын
Man, great job with that explanation on the whiteboard. Having a mental model for this problem is hard enough, but explaining each step and having to make all those edits into a video must've been really tough. Thanks for your hard work!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
nice - thx
@pasankalansooriya3124
@pasankalansooriya3124 4 жыл бұрын
Very attractive teaching style.good job sir
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks
@yicai7
@yicai7 4 жыл бұрын
You are my Prof. Thanks for the awesome vid! Voted!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@RahulKumar_gr8
@RahulKumar_gr8 4 жыл бұрын
There's a difference between understanding something and being able to make others understand the concept. Great job ! I Understood the concept of backtracking to a great extent.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
great
@Justin-cn3qu
@Justin-cn3qu 5 жыл бұрын
I just wanna say, great job.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
thx
@jeongtaebang3679
@jeongtaebang3679 5 жыл бұрын
For time complexity, can you please explain beyond the fact that there are n! permutations that need to be computed? I think the number of recursion calls is more than that, but not sure how to formally count this (also not sure if interviews will ask for that level of mathematical formalism)
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
The recurrence for generating permutations recursively is: T(n) = n * T(n-1) + O(1) where we just print the permutation (we don't copy a string to an answer list or anything). In each call, we will make n calls and n will reduce in size by 1. Each call will do at worst O(1) work. If n = 4 then we will see it concretely solves to n * (n - 1) * (n-2) * (n-3) where T(1) is a base case entailing a print. Does this make sense?
@MarsTheProgrammer
@MarsTheProgrammer 4 жыл бұрын
Your so fast paced that i got dizzy and nauseous watching you (plus with all the cuts), sorry couldn't follow anything you said..
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
no problem - old video, sorry
@KrishnaKumar-jr7qq
@KrishnaKumar-jr7qq 3 жыл бұрын
7:55 felt like watching Edge Of Tomorrow. LOL! But its great !!
@BangMaster96
@BangMaster96 3 жыл бұрын
This must have been exhausting to film.
@Geopoliticalguy-z8n
@Geopoliticalguy-z8n Жыл бұрын
Is there any alt of recursion. Seriously this recursion is killing me...😢😢
@biniyamasnake9756
@biniyamasnake9756 5 жыл бұрын
Good video. Learned the theory. It would have been nice to SEE the code and discuss tradeoffs of the different ways of programming the backtrack algorithm which is what I wanted but I like how you mentioned some of those ways.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
Yeah. I realized this after I did it. Check this out: github.com/bephrem1/backtobackswe/issues/1 I will give this code and maybe do another video.
@melaniemeza2139
@melaniemeza2139 4 жыл бұрын
You are awesome!!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thx
@chenchen55688
@chenchen55688 5 жыл бұрын
I'm wondering if the time complexity is calculated as the total number of function calls, it would be exactly the number of nodes been traversed in the "tree." So that would be counted in such manner(from top of the tree, each term represents the number of nodes on each layer): N + N(N-1) + N(N-1)(N-2) … + N(N-1)(N-2)…(3) + N(N-1)(N-2)…(2) + N!, and this gets to e*N! as N gets large. Please correct me if I'm wrong. Thanks!
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
I'm not fully clear on the format of what you are summing
@chenchen55688
@chenchen55688 5 жыл бұрын
@@BackToBackSWE this might be clear imgur.com/a/dkUb8V1
@iggykarpov
@iggykarpov 4 жыл бұрын
Watching this at 0.75 speed seems pretty good. :)
@BackToBackSWE
@BackToBackSWE 3 жыл бұрын
thx sorry old vide
@supremoluminary
@supremoluminary 4 жыл бұрын
Thanks for that. I would like to see a code walkthrough next.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@dernyt-tpe
@dernyt-tpe 4 жыл бұрын
A big thank you 😊
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@saurabhdeshpande8450
@saurabhdeshpande8450 4 жыл бұрын
woahh....great energy man!!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
old video - sorry
@bibiworm
@bibiworm 3 жыл бұрын
He is one of the few people on KZbin who actually elaborate the concept of backtrack clearly. Great job!
@Luka-se7sl
@Luka-se7sl 4 жыл бұрын
Thanks God, that in word `boat` is 4 letter and not 5
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
yeah
@beverlyukandu7189
@beverlyukandu7189 4 жыл бұрын
Suggestion: Try to stand on the opposite of the board you are writing. It's tough sometimes to take notes when you are standing directly in front of what you wrote out. Great job otherwise.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
ok
@JN-wj5tj
@JN-wj5tj 4 жыл бұрын
ha ha! The artistic license with backtracking in the video is cute. The jumpiness of the video get's a little much. However, overall, the language used is clear and that makes understanding the approach easily understood ! Well done!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
"The artistic license with backtracking in the video is cute." what and yeah sorry old video. and nice, surprising this one was rough, and thanks
@akashm103
@akashm103 4 жыл бұрын
why boat with 24 permutation , why not cat with 8 permutation, would have made your job easy.......... but hats off to your energy
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
I don't remember this video too much in detail, old video, bad mic, I was loud
@boredasever6645
@boredasever6645 4 жыл бұрын
why are you so frustrated with criticism in the comments. just don't reply it looks bad lol.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
I don't remember what I said, this video is old. And that is fine.
@242deepak
@242deepak 4 жыл бұрын
If you would have not erased and drawn full recursion tree the understanding would have been better
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
yes
@CodeSuccessChronicle
@CodeSuccessChronicle 3 жыл бұрын
you're DS god 🙏🙏🙏 love from India ❤
@HeWhoShalNotBeNamed_
@HeWhoShalNotBeNamed_ 4 жыл бұрын
huge respect for such massive amount of efforts exerted. very much appreciate them! for a beginner in recursion, very very helpful.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@frozen_tortus
@frozen_tortus 4 жыл бұрын
These permutations kind of remind me on generative recursion. Backtracking, I learn is for search? This bothers me sometime now.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
ye
@bhushanshinde8358
@bhushanshinde8358 4 жыл бұрын
Love ur energy n enthusiasm to get to depth of a problem rather than just solving the problems... Good job!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@kuralamuthankathirvelan
@kuralamuthankathirvelan 5 жыл бұрын
Very Clear explanation , Thanks a lot bro . Looking for more DP videos .
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
sure
@eve7947
@eve7947 5 жыл бұрын
What if there are duplicate elements in the array, how do you avoid generating duplicate array for the answer?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
We could just put the permutations in a HashSet but then again there is probably a smarter way to prune the placements going downwards. Reminds me of: leetcode.com/problems/subsets-ii/ I'll think on this.
@roeekimmel
@roeekimmel 3 күн бұрын
At 11:00 I definitely could feel the mental struggle
@backtobasics6865
@backtobasics6865 4 жыл бұрын
it would be good, if you can explain wrt code
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
yeah
@divyanshuverma5652
@divyanshuverma5652 4 жыл бұрын
Sad that you are also like other KZbinrs as you are also solving the problem not explaining the code. I think code explanation is much much better but sadly I have to pay everywhere to get the code explanation.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
I explain this in other videos and yeah people charge for (a minority share of) their work shocker
@divyanshuverma5652
@divyanshuverma5652 4 жыл бұрын
@@BackToBackSWE Firstly thanks for replying and Yes people should charge for there work as they are spending a lot of time and effort but from a student point of view there are so many places to study but only some of them are good (like yours) that students like me got confused how to study and where to pay the charges as in starting everything works fine but at the end, it's all messed up and being a student its hard to pay everywhere to get the materials and learn. But you are doing a great job. Big thumbs up for you. 👍😁
@user-zd1hj2lv3n
@user-zd1hj2lv3n 4 жыл бұрын
When recursion makes that gym membership pay off 😂 Great explanation. Thanks
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
wut
@mridulmittal5953
@mridulmittal5953 5 жыл бұрын
never seen this much energy ! thanks dude
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
I'm a wild guy
@riaagnes353
@riaagnes353 5 жыл бұрын
Can you provide the code? I could not find the link.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
yeah, it's been open on the github for a while as an issue
@abudibro200
@abudibro200 3 жыл бұрын
Didn't know alexandre lacazette was a software engineer
@afeaddeh2203
@afeaddeh2203 4 жыл бұрын
If we used a StringBuilder instead of a Strings would the time efficiency be O(n!) and not O(n *n!)? I think that with StringBuilder we can just append the 'choice' in each frame instead of deep copying the String?
@BackToBackSWE
@BackToBackSWE 3 жыл бұрын
Im not sure I dont remember
@PARUS1978
@PARUS1978 4 жыл бұрын
Every time I listen your video I think need to set playback speed to 0.75%
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sorry - old video i understand
@adityamaurya8092
@adityamaurya8092 4 жыл бұрын
Can we solve this problem without recursion
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
yes, but I don't remember how its coded. you can probably find a solution online
@richduchin4098
@richduchin4098 4 жыл бұрын
dude...keep it up! I could care less about the negative comments, ignore them. As a very seasoned software engineer, company founder, and exceptional teacher, I can safely say you have described this in the most laymen way possible. Excellent! Would love to connect with you and discuss a couple of things. Reach out to me on linkedin
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
This video is old haha, are the comments bad? I don't notice. I just respond to the stream that hits my notification bell. I don't use LinkedIn much, maybe email me? backtobackswe.com/contact
@evanvinet8773
@evanvinet8773 3 жыл бұрын
this is not helpful at all. I was hoping to learn some string commands not some extremely basic theory
@andrejivanovic8989
@andrejivanovic8989 5 жыл бұрын
this is dope you're doing a great job, keep it up
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
a lot of work ahead this year
@z08840
@z08840 3 жыл бұрын
...I think it's about chinese room argument, but this is rather white board room...
@airysm
@airysm 3 жыл бұрын
Is your old github repo with the solutions still available or is that behind Backtobackswe.com now?
@rndmvds9765
@rndmvds9765 4 жыл бұрын
Here's my question. What if there is repeating letter in a string?
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
I don't remember but these's a way
@aleyummusic
@aleyummusic 5 жыл бұрын
Posting the code for this would be helpful
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
github.com/bephrem1/backtobackswe/issues/1 ;)
@VinodKumar-pf1un
@VinodKumar-pf1un 4 жыл бұрын
what if the string length is 50 or something. Is there any way to calculate all the permutations in an efficient way for that case ?
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
I'm not sure
@lianghe1401
@lianghe1401 3 жыл бұрын
You are just so good at explaining those sort of ideas! The beauty of is I don't have to finish the video to get your idea. I stopped the video at 5 minutes and able to go back and resolve my problem.
@BackToBackSWE
@BackToBackSWE 3 жыл бұрын
great to hear!
@krishnakrmahto97
@krishnakrmahto97 5 жыл бұрын
"No more decisions to make, backtrack" - this one was nice though.. :P
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
thanks
@anujagrawal60
@anujagrawal60 4 жыл бұрын
Generally i don't comment, but dude this explanation is exceptional.I never understood string permutations this better. great work
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
welcome to commenting lol & thx
@JOHNSMITH-ve3rq
@JOHNSMITH-ve3rq 2 жыл бұрын
Definitely can't say 'do you even lift bro?' to this guy
@fantasy9960
@fantasy9960 2 жыл бұрын
wow thank you for such a great video!
@virajkamat2764
@virajkamat2764 4 жыл бұрын
Time complexity of video O(Now!)
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
yes
@kevinpatel5106
@kevinpatel5106 2 жыл бұрын
you are awesome, I watch a lot of your videos for help!
@MiddleEasternInAmerica
@MiddleEasternInAmerica 4 жыл бұрын
superb 🔥🔥🔥🔥🔥🔥🔥🔥
@BackToBackSWE
@BackToBackSWE 3 жыл бұрын
thx
@kylenelandenberger6482
@kylenelandenberger6482 4 жыл бұрын
This is awesome. I totally understand it better than before. I am having a hard time putting this into recursion though. do you mind making the code live again?
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
Thanks and the repository is deprecated - we only maintain backtobackswe.com now.
@sim77778777
@sim77778777 5 жыл бұрын
One of the best tutorials I have ever seen. Great job
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
Thanks
@sergeivashchenko7308
@sergeivashchenko7308 5 жыл бұрын
Great job man, you are really good at explaining
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
thanks!
@EduSadiq
@EduSadiq 4 жыл бұрын
What happened to you sir! You are speaking and delivery lecture extremely fast! You should deliver your lecture little bit slower... Your previous lecture were nice...
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
ok, let me go back and fix this, brb
@AllanPichardo
@AllanPichardo 4 жыл бұрын
slow down lol
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
Old video - sorry. Things have to start somewhere.
@ponrajkumarp6777
@ponrajkumarp6777 4 жыл бұрын
how is space complexity O(n*n!) when we store in an array. we are storing the result so it should take O(n!). can you please explain. Thanks for the explanation.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
I don't remember this to be honest
@namanmishra3778
@namanmishra3778 4 жыл бұрын
Well, I guess n! is for the permutation purpose and n to print each permutation so that makes it O(n*n!). Not sure!!
@ponrajkumarp6777
@ponrajkumarp6777 4 жыл бұрын
@@namanmishra3778 Yea Whatever you are saying is for time complexity. but my doubt is about space complexity dude
@snoopaku
@snoopaku 4 жыл бұрын
Great explanation, thank you so much! I have a question about the space complexity when we just print our permutations. The worst case is when we reach the bottom of our recursion and that would be O(n) because of n stack frames, I get it. But every stack frame also has its current state, I mean current permutation, which is also of length n in worst case. Am I right? Shouldn't space complexity in this case be O(n*n) = O(n^2)? Thanks a lot!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
Yes, that would be the case, but if you pass a reference to a single mutable string (maybe make your own char array) you can keep the space linear (no copies in each frame of a whole structure).
@snoopaku
@snoopaku 4 жыл бұрын
@@BackToBackSWE Yeah, you're right, thank you!
@soft-tech7233
@soft-tech7233 4 жыл бұрын
super>>
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks
@jazzbandpiano
@jazzbandpiano 3 жыл бұрын
the question is, how do you transfer your "running string" down the call stack without making a deep copy of it each time? Requires some trickery...
@ZachWalz
@ZachWalz 5 жыл бұрын
In your example, how would I figure out that “obat” is the 8th permutation without writing out all possible permutations that came before it?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
So our original string will be "boat" . I immediately know that in the 1st slot the "b" will go first with a remainder of "oat", then the "o" with a remainder of "bat", then the "a" with a remainder of "bot", then the "t" with a remainder of "boa". That is what the first positioning will yield. So...4! = 24. We know that we will have 24 total permutations (for reasons I think this video explains...I did this a while back so not sure). So go back to what I was saying...."b" will go first with a remainder of "oat". With "b" in slot 1 (and 3 remaining slots)...I know for sure that I will be able to reap 6 permutations. Why? Well...3! = 6. So ok....we know that the 7th permutation will start with the "o" placed in the first slot with a remainder of "bat". So from there we can just do the enumeration and get there faster... We hopped to the 7th...and that is: o b a t _ _ _ _ If you followed this whole explanation then you should have a deeper understanding of what is happening.
@vinnygouda2750
@vinnygouda2750 4 жыл бұрын
Take a bow.......my brother what an effort
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
ye
@VeereshPradeep
@VeereshPradeep 5 жыл бұрын
Man there's some problem with this video. It just gets stuck buffering after first 20 seconds.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
hm
@ezpz4akash
@ezpz4akash 5 жыл бұрын
Seriously Man 😵
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
@@ezpz4akash yuh
@chandraprakashsingh1039
@chandraprakashsingh1039 5 жыл бұрын
while writing the code you are looping over l to r but in helper fuction u will pass l+1 not i+1 like your rest of the backtracking videos.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
where?
@chandraprakashsingh1039
@chandraprakashsingh1039 5 жыл бұрын
@@BackToBackSWE void util(vector &A,vector &v,vector &sub,int data){ // if(data==A.size()){ v.push_back(sub); for(int i=data;i
@chandraprakashsingh1039
@chandraprakashsingh1039 5 жыл бұрын
but, in case of string permutation void permute(string a, int l, int r) { // Base case if (l == r) cout
@wl7497
@wl7497 5 жыл бұрын
Great job! so for array permutation problem, the time complexity should O(n!), space complexity is O(n), n is the size of input array.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
Which version?
@mdsumon-ee5ke
@mdsumon-ee5ke 3 жыл бұрын
"exhausted all possibilities of x, no more backtrack"!!. Love your effort man, really. I finally understood backtracking! Yeah, I needed explanations 24 times! thank you :)
@saruncse85
@saruncse85 5 жыл бұрын
Nice Explanation. Thank you.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
sure
@tmorid3
@tmorid3 3 жыл бұрын
You are great!!! 1 important thing - please put links to the previous videos of yours that you are referring to. It would really help. thanks!!
@rodolfom.r9146
@rodolfom.r9146 2 жыл бұрын
Wow dude i had seen lot of tutorial but yours its the most amazing , clear and comprehensive ,thanks for take the time to do this , im subscribed now , greetings
@pradeexsu
@pradeexsu 4 жыл бұрын
thank you sir for great job. : ) plz make tutorial on the case when reapting char are there in string without reapting permutation like "Boom". it printed twice. how to avoid this reaptation without using "HashMap" and "Set".
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
ok
@duhudu
@duhudu 5 жыл бұрын
Thanks sir, first 4 minutes and got the idea how to solve it.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
nice
@shubhamjindal9214
@shubhamjindal9214 5 жыл бұрын
exactly same here ....! watched the video till 4:07 mins
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
@@shubhamjindal9214 haha nice
@abdelrahman2348
@abdelrahman2348 5 жыл бұрын
What is the difference between this problem and 6.9 in aziz's book ?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
what was 6.9? I don't have the book up with me rn. I had to wipe my hard drive (had issues) and I'm setting shit back up again now...so yeah...I assume it is the same problem using an array? A string is an abstraction, it is an array of characters. It is a sequence. Any sequence can be permuted.
@abdelrahman2348
@abdelrahman2348 5 жыл бұрын
Back To Back SWE it’s okay and thanks for your attention...the problem of chapter 6 number 9 is Give an array A of n elements and a permutation p, apply p on A.. this part is easy to implement but the point is he said in the book any permutation can be viewed as a set of cyclic permutations for each element in the cycle how’d you identify if it has been permuted ..I didn’t understand what does it mean
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
@@abdelrahman2348 OHHHH, just pulled it up. Not it is not the same. And yeah, I just read the "Hint: Any permutation can be viewed as a set of cyclic permutations. For an element in a cycle, how would you identify if it has been permuted?" For this problem they say "A permutation can be specified by an array P". This is different from what we call a permutation in this problem. A permutation in that context is the codification of final positions for items. The "Hint" is that each permutation represents a series of interlinked steps to get all elements to their final positions so that O(1) space can be used (since it is trivial to perform the repositionings into a new array). These interlinked swaps form a series of "cycles" where we must swap an element in...then swap the element that sat where we just swappped into its final place...and so on... That is an odd way of saying it but yeah...that's the gist of it.
@abdelrahman2348
@abdelrahman2348 5 жыл бұрын
@@BackToBackSWE Thank you, but I still can't figure out it the permutation cycle , could you discuss it later in a video ?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
@@abdelrahman2348 possibly
@juniorcoder6988
@juniorcoder6988 5 жыл бұрын
GREAT !!! I liked your lectures !
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
thanks
@cocoarecords
@cocoarecords 5 жыл бұрын
thanks !!
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
sure
@ridhwaans
@ridhwaans 5 жыл бұрын
what does a naive solution to this look like? without backtracking
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
As in an iterative approach? How would you brute force generating permutations?
@norenpotsangbam7093
@norenpotsangbam7093 4 жыл бұрын
Thanks bro really helpfull
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
Sure
@atlantean.prince
@atlantean.prince 3 жыл бұрын
I like this guy
@kishorkumarsingh9072
@kishorkumarsingh9072 5 жыл бұрын
Can i borrow some of your energy?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
yeah, how much u want
@bronzebond4869
@bronzebond4869 Жыл бұрын
outstanding job dude. Over a year and i still go back to this
@sanjivmadhavan5705
@sanjivmadhavan5705 5 жыл бұрын
man checking abs often:")
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
wat
@dhee01
@dhee01 3 жыл бұрын
Yoo.Thanks my man.This was really helpful.
@gregoryrobertson
@gregoryrobertson 2 жыл бұрын
I really enjoy learning from you, thank you for posting this.
The N Queens Problem using Backtracking/Recursion - Explained
14:29
Back To Back SWE
Рет қаралды 137 М.
The Change Making Problem - Fewest Coins To Make Change Dynamic Programming
23:12
Minecraft Creeper Family is back! #minecraft #funny #memes
00:26
From Small To Giant Pop Corn #katebrush #funny #shorts
00:17
Kate Brush
Рет қаралды 72 МЛН
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 11 МЛН
The 0/1 Knapsack Problem (Demystifying Dynamic Programming)
20:30
Back To Back SWE
Рет қаралды 205 М.
Characters, Symbols and the Unicode Miracle - Computerphile
9:37
Computerphile
Рет қаралды 2 МЛН
17 CS 106B Lecture Backtracking permute a string
22:05
Marty Stepp CS106 CS193 Stanford Archive
Рет қаралды 8 М.
BACKTRACKING: How to solve (almost) any problem.
12:49
Santiago Fiorino
Рет қаралды 54 М.
How to Write a Paper in a Weekend (By Prof. Pete Carr)
11:39
Surviving and Thriving in Higher Education
Рет қаралды 2,2 МЛН
What's The Longest Word You Can Write With Seven-Segment Displays?
8:56
FizzBuzz: One Simple Interview Question
7:18
Tom Scott
Рет қаралды 3,5 МЛН
Minecraft Creeper Family is back! #minecraft #funny #memes
00:26