Letter Combinations of a Phone Number - Backtracking - Leetcode 17

  Рет қаралды 164,630

NeetCode

NeetCode

Күн бұрын

Пікірлер: 156
@NeetCode
@NeetCode 3 жыл бұрын
🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤
@themagickalmagickman
@themagickalmagickman Жыл бұрын
I'd suggest putting this as the first problem in your backtracking list on neetcode list as its the most straightforward I think
@fredtrentini6791
@fredtrentini6791 Жыл бұрын
Definitely agree, I trained so much with the previous backtracking problems that when I got to this one I solved it within 6 minutes and was like: wait, that's all? (btw for the sake of comparison some of the previous problems took me like 30+ minutes to solve)
@arpitakar3384
@arpitakar3384 11 ай бұрын
@@fredtrentini6791 i thought i was the only one hustling this long with 30 lines of code writing.
@tonyiommisg
@tonyiommisg 10 ай бұрын
@@fredtrentini6791 totally agree. I thought I had done something wrong when I solved with this one as it was so much easier I thought I was missing something.
@rohanmahajan6333
@rohanmahajan6333 4 ай бұрын
nah I feel like you definitely need to know whats up to be able to solve this. I was able to (not to toot my own horn) but I felt significantly more proud of solving this than i did for like combination sum ii or something
@leonscander1431
@leonscander1431 3 ай бұрын
Unfortunately he's not taking our wishes into account. It's still in the end. And Generate Parenthesis from Stack section should be in Backtracking too.
@PAIPENG-c2b
@PAIPENG-c2b 11 ай бұрын
for those who might get confused by pop(), here the size of the return list are always the same, so it doesnt need to pop the element to give space for the next one. It just need to straight forwardly add all the possible subsets to the result
@sjl006
@sjl006 2 ай бұрын
For those who wondered why there's no append or pop (similar to the other backtracking approaches) it's because strings are immutable. Everytime the call is made to the backtrack method, python creates a new string so when the method returns up the stack, the caller still has the original string without the concatenated letter.
@rijulranjan8514
@rijulranjan8514 Ай бұрын
This was confusing me, thanks a lot!
@baap6886
@baap6886 2 жыл бұрын
best coding channel for DSA and best approaches. Love From India❤
@jiwachhetri4165
@jiwachhetri4165 2 жыл бұрын
His explanation makes it so easy to understand even for beginners
@jay-rathod-01
@jay-rathod-01 3 жыл бұрын
NeetCode your channel is going to be at the top someday. Good explaination and good code.... But one request: Whenever you get time. Just partition this playlist into either Data structure or algorithmic approach paradigm. Instead of just naming it Coding interview solutions.
@TheModernPolymath
@TheModernPolymath 2 жыл бұрын
that day has come 😁
@jasonbrody4618
@jasonbrody4618 Жыл бұрын
sale joy. mera future dekh ke baata de @jay-rathod-youtube
@PippyPappyPatterson
@PippyPappyPatterson Жыл бұрын
@@TheModernPolymath ur mom has come [1] [1] kzbin.info/www/bejne/p3LWap-XrahgqNU
@The6thProgrammer
@The6thProgrammer Жыл бұрын
The time complexity would be worse than O(N*4^N) because you are passing curStr by value which means for each call to backtrack() you are performing a string copy. If you passed currStr by reference then N*4^N would be correct (but then you would need to also remove elements from the end of the string when backtrack() returned using pop). And for anyone still confused about the N in the time complexity given, at each of the solutions (of which there are at most 4^N), we are copying a string of length N to the result which is a linear time operation. So O(4^N * N).
@AbhijithVMohan
@AbhijithVMohan 7 ай бұрын
This is not true for python. There is no separate pass by value & pass by reference in python as in C++. It's only pass by reference (to be pedantic, a reference is passed by value - same in java). The issue here is string concatenation.
@IsomerMashups
@IsomerMashups 3 жыл бұрын
I've learned not to trust leetcode's runtime number because I ran the same algorithm twice and the first time it put me in the bottom 5% and the second it put me in the top 25%.
@CknSalad
@CknSalad 3 жыл бұрын
yeah, there's actually a meme about having the phone tech interviews at a certain time if they straight up use leetcode because less server latency hit lmao.
@theraczcar
@theraczcar 3 жыл бұрын
This helped me with getting through a coding problem to find out possible pins from a keypad! Thanks a ton this makes recursion a little less scary!
@Chansd5
@Chansd5 2 жыл бұрын
Recursion's a total beast. It scares me. But I can do anything if Neetcode babysteps me little by little :)
@imakeawfulmusic
@imakeawfulmusic Жыл бұрын
@@Chansd5 fr fr, It demotivated me so much that i had stopped doing leetcode problems. Then, i found NeetCode *heavenly music plays*
@MrjavoiThe
@MrjavoiThe Жыл бұрын
This actually makes it more confusing to me 😢
@jkk23-g7c
@jkk23-g7c 4 ай бұрын
I somehow coded the solution myself, but wasn't sure why it worked. You have the best explanations
@jamestruong3320
@jamestruong3320 6 ай бұрын
Hi Neet, loved the solution- it was very concise and simple. One thing I would add to the explanation is that you made it seem like it was a breadth first solution instead of a depth first solution inside your diagram segment. The for-loop executes after each backtracking return, not when they are called. In other words, the next iteration of the for loop is only executed after the first completed string.
@oofy9103
@oofy9103 2 жыл бұрын
Similar idea and coding as #78 Subset but more easier. Thanks so much for the explanations!
@albertlee5648
@albertlee5648 Жыл бұрын
I think it's more efficient to append a letter to a list and join at the end of the recursions than to add strings since string is immutable
@sharoncohen318
@sharoncohen318 Жыл бұрын
This solution beats 19%, I did this iteratively and it beat 96%.
@Chansd5
@Chansd5 2 жыл бұрын
This is not on your Neetcode 150 nor Blind 75, but I was practicing this as a variation of "Generate Parentheses" and I'm delighted you have a solutions video! Amazing content for normal, non geniuses like me who struggle through these problems! You sir, are a live saver!
@torin755
@torin755 2 жыл бұрын
It is on neetcode 150, it's under backtracking as the 2nd last
@Chansd5
@Chansd5 2 жыл бұрын
@@torin755 Gotcha! I'm still working on "Stacks" and on my way down.
@akashverma5756
@akashverma5756 Жыл бұрын
Earlier, Backtracking was nightmare for me. Now, It is easier than any algorithm I have learned till now.
@AnnieBox
@AnnieBox 2 жыл бұрын
backtracking is always my painpoint but everything looks so neat and easy to understand from you video!👍🏻
@rajeshmadira8306
@rajeshmadira8306 3 жыл бұрын
Excellent explanation, Please try to explain more problems per week
@zehuazhou3390
@zehuazhou3390 3 жыл бұрын
Have you considered doing an iterative solution? I think it is doable. Take the "23" input for example, we can init our index array to be [0,0]. Then we would output "ad" first, because the first 0 maps to a in [a,b,c] array, and the second 0 maps to d in [d,e,f] array. Then we can add 1 to 00, which becomes 01, and this gives us ae. Then add 1 again, and it becomes 02 and we get af. Then add 1 again and then it overflows because [d,e,f] does not have index 3 in it, so we reset this to 0 and increment the first 0 to 1, and it changes from 02 to 10. We complete the loop when the first digit overflows.
@zehuazhou3390
@zehuazhou3390 3 жыл бұрын
I just had a try with the above iterative plan and the running time and memory usage are better: Runtime: 1 ms, faster than 76.76% of Java online submissions for Letter Combinations of a Phone Number. Memory Usage: 37.9 MB, less than 75.15% of Java online submissions for Letter Combinations of a Phone Number.
@JackAbou2
@JackAbou2 2 жыл бұрын
@@zehuazhou3390 can you share your code? thanks
@09sangram
@09sangram Жыл бұрын
this the only tech foreign channel I follow from India. Good going
@PippyPappyPatterson
@PippyPappyPatterson Жыл бұрын
ur mom's from india
@Jun-zq3bn
@Jun-zq3bn Ай бұрын
should be "pqrs" for 7 instead of "qprs"
@shreekarbukkapattanam3441
@shreekarbukkapattanam3441 3 жыл бұрын
great approach but is this backtracking!!?? i think its recursive only
@pedrov8868
@pedrov8868 2 жыл бұрын
Correct, still a nice approach
@Pranav-nn1lu
@Pranav-nn1lu 2 жыл бұрын
I mean even if there is no dedicated backtracking funciton call, isn't it technically still backtracing because we are appending a new character to 'curStr' in the function call itself and everytime that funciton is popped from the recursion stack, the character that we last appended gets removed automatically and hence we essentially just backtracked.
@The6thProgrammer
@The6thProgrammer Жыл бұрын
I think there is a lot of confusion around what backtracking is. Just because you explore the entire set of possibilities and all of them are valid does not mean an approach does not use backtracking. "Pruning" can make backtracking algorithms more efficient by eliminating invalid solutions as they are encountered but is not required for backtracking. From Geeks for Geeks: "Backtracking is a problem-solving algorithmic technique that involves finding a solution incrementally by trying different options and undoing them if they lead to a dead end." The dead end in this case is simply the length of the candidate strings we are building.
@janaSdj
@janaSdj 6 ай бұрын
​@@The6thProgrammercorrect
@Dhruvbala
@Dhruvbala 5 ай бұрын
It can be, if you use a list of characters and ‘’.join(characters) in the base case - instead of passing strings. Doing so is more time and memory efficient
@Thisismyworld123
@Thisismyworld123 3 жыл бұрын
Hey.. great solution to solve this problem but it doesn't use the concept of backtracking. You are currently parsing the whole recursion tree without pruning it. Pls update!
@umberto3271
@umberto3271 2 жыл бұрын
why is the time complexity O(n * 4^n) and not just 4^n, where we have 4 choices at a height of n
@PippyPappyPatterson
@PippyPappyPatterson Жыл бұрын
Yeah, I get that it's more than 4^n, but I'm not convinced that it's n * 4^n.
@The6thProgrammer
@The6thProgrammer Жыл бұрын
At each of the 4^n solutions we copy a string of length n to a list which is a O(n) operation. So for all 4^n leaf nodes in the tree we do O(n) work which is n*4^n.
@zackgrey4472
@zackgrey4472 Жыл бұрын
I'm finally getting backtracking algorithms in < 20m :-.) maybe I'll actually be able to get a job, tysm Leetcode king
@shehabeldinadel8531
@shehabeldinadel8531 3 жыл бұрын
First thing I thought about is a graph problem with an Inorder traversal
@illu1na
@illu1na Жыл бұрын
Each backtracking, seems like the solution is copying the entire string again and again. So its O(n*4^n) ? for char in digit_map[digit]: curr.append(char) recur(i+1, curr) curr.pop() this would be better like other backtracking solution. Perhaps this question was done before the other questions
@luanlucas8605
@luanlucas8605 3 жыл бұрын
Great explanation, please keep it up! :)
@nazarzimarev8657
@nazarzimarev8657 8 ай бұрын
Amazing.... Didnt think that we can use recursion
@yuanliu200
@yuanliu200 3 жыл бұрын
So neat, this deserves a lot more likes
@Fanaro
@Fanaro 8 ай бұрын
Shouldn't you, for example, save in the dict patterns such as "23" in the input string "2323"? I pretty much treated this problems as a hash map problem, and got way better results.
@jsdev6744
@jsdev6744 2 жыл бұрын
actually I'm curious, would this solution be considered as "backtracking" ? as we aren't returning the value once the base case is hit, but rather building each solution as we get further into the recursion calls. Obviously nothing major, just trying to solidify my concepts. I realize some recursion solutions are built as we get deeper into the call, and other solutions are returned one by one after hitting the base case.
@hamoodhabibi7026
@hamoodhabibi7026 2 жыл бұрын
So backtracking is recursion. The reason why this solution is considered backtracking is because regardless of hitting a base case or a solution where we return earlier (i think it's called pruning) we still have to complete the tree and go through every possible combination.
@BobBob-e
@BobBob-e Жыл бұрын
yes i was thinking the same thing this does not seem like backtracking which is kind of misleading
@PippyPappyPatterson
@PippyPappyPatterson Жыл бұрын
@@hamoodhabibi7026 I think of non-backtracking recursive solutions as DFS solutions.
@alexisacosta6758
@alexisacosta6758 5 ай бұрын
At first I was confused why we didn't pop from curStr after the call to backTrack(). Can someone check my reasoning here? We don't need to pop from curStr because curStr + c implicitly creates a copy of curStr with c appended to it. Therefore, each recursive call gets a different copy of curStr so no clean up (popping) is needed. This is in contrast to problems where each recursive call gets an array passed to it. Since, in those problems, each recursive call gets the same reference to the array. Meaning that clean up is needed because you don't want values from this recursive call to be present in other calls.
@srivardhan.s5191
@srivardhan.s5191 4 ай бұрын
correct
@yimengjiang9614
@yimengjiang9614 2 ай бұрын
yea, we have to pop if using Java with StringBuilder -- curr is a reference type and pass in a reference so we need to change it back manually.
@ravipatel-xu5qi
@ravipatel-xu5qi Жыл бұрын
I would suggest that you should have explained this with input "234"
@EddieCheng174
@EddieCheng174 Жыл бұрын
I am curious where is the backtracking part of this question 🤔
@NinjiaJoeLife
@NinjiaJoeLife 2 жыл бұрын
why you classify this question as a "backtrack" question? It looks like just a regular dfs to me. For backtrack, we usually have those types of "append-pop" thing
@atishayjain3011
@atishayjain3011 Жыл бұрын
Sorry I'm late but I'm commenting for my own understanding as well. The reason we do the append pop thing is because we're using a shared set/list which is getting passed down. So, when we go up the decision tree, we need to make sure to pop the current elements we've used. However, when we're using strings, we don't need to do that since a new String is created for each recursive call. The string in the original recursive call hasn't changed as a result of the calls.
@PippyPappyPatterson
@PippyPappyPatterson Жыл бұрын
@@atishayjain3011 So… still not backtracking kek
@atishayjain3011
@atishayjain3011 Жыл бұрын
@@PippyPappyPatterson What do you mean? It is absolutely backtracking.
@PippyPappyPatterson
@PippyPappyPatterson Жыл бұрын
@@atishayjain3011 > we don't need to do that [backtracking] since a new String is created for each recursive call
@atishayjain3011
@atishayjain3011 Жыл бұрын
@@PippyPappyPatterson It is still backtracking. The only difference is the manipulation. We don't need to have an append/pop thing, but the algorithm still works in a backtracking way. Backtracking is more a concept than an implementation.
@LotusTree-jw1mr
@LotusTree-jw1mr 2 жыл бұрын
Great video! Can I please ask how did you make a video like this? What tools did you use? thank you!
@akhileshb5859
@akhileshb5859 2 жыл бұрын
Hey Neetcode, I have one question! but before that a big thanks from bottom of heart for your wonderful solutions, this helps a lot. so my question is how do you come up with such a super precise solutions, are you born intelligent or is it because of lot of practice? how?? I want to know.. even I want to come with such good solutions with out having to watch your video's. Thanks anyway:)
@pyserialkiller110
@pyserialkiller110 Жыл бұрын
It's undeniable that my boy here is quite smart, but it is also true that with enough effort you can learn the patterns and start recognizing then when reading a problem. It's all practice and persistence bro!
@Mankind5490
@Mankind5490 2 жыл бұрын
I was asked this question at Facebook interview.
@Eggleweb97
@Eggleweb97 2 жыл бұрын
Wow
@joshgung
@joshgung Жыл бұрын
This question is asked today to me in amazon interview :) Could not answer :D
@zhouwang2123
@zhouwang2123 3 жыл бұрын
I like your channel and voice as well, lol...
@AbanoubAsaad-YT
@AbanoubAsaad-YT 2 жыл бұрын
Thank you for the explanation/solution! I always forgot drawing the decision tree :(
@raychang6443
@raychang6443 2 жыл бұрын
I think "curStr + c" costs O(n), so it's actually more than O(4^n*n) unless you only join the string in the end.
@cagrkaymak3063
@cagrkaymak3063 2 жыл бұрын
I agree, I think with string concat logic, it should be O(4^n * n^2), if we use a list to store the string, then the complexity would be O(4^n*n)
@abielkim960
@abielkim960 2 жыл бұрын
can someone please explain why the time complexity is O(n*4^n)? and not O(4^n)? Thank you.
@kyayaarbankey
@kyayaarbankey 2 жыл бұрын
4^n is for the worst case scenario but the length of the string that we will be returning in the list will be of length 'n', same as the input string length. Therefore the time complexity is O(n*4^n)
@wagmore6455
@wagmore6455 2 жыл бұрын
The complexity is O(4^n). It's a "permutation with repetition" which is r^n, where r is the number of options you have for each digit and n is the number of digits. The "worst case" means that we use 4 rather than 3, since the 7 and 9 buttons have 4 characters we assume all buttons have 4 characters.
@hamoodhabibi7026
@hamoodhabibi7026 2 жыл бұрын
"4^n" is every combination that is made in a decision tree (in worst case Example: "999..9") multiplied by "n'" the actual length of each output string (which is the same length as our input string n)
@PippyPappyPatterson
@PippyPappyPatterson Жыл бұрын
@@hamoodhabibi7026 but you don't perform `n` operations for each of the `4^n` combinations
@The6thProgrammer
@The6thProgrammer Жыл бұрын
The simple explanation to your question is at each of the 4^n solutions we copy a string of length n to a list which is a O(n) operation. So for all 4^n leaf nodes in the tree we do O(n) work which is n*4^n.
@orad23
@orad23 2 жыл бұрын
wish you would go over base cases in your code. It would be helpful if you go over it in real time
@yilmazbingol4838
@yilmazbingol4838 3 жыл бұрын
if not digits: return [] write this at the beginning of algorithm, you will get much better result
@BobBob-e
@BobBob-e Жыл бұрын
this is not backtracking??? there is no code that undos your choice so i think it is more accurate to say that it just traverses the entire graph??? i think for backtracking you would want to keep a record of used values so that you can skip them and be able to reverse your choices whereas here you do not have to since you are only going deeper into the graph
@PippyPappyPatterson
@PippyPappyPatterson Жыл бұрын
A lot of these he calls backtracking really seem to be just a depth-first traversal.
@freeadvice-tamil24
@freeadvice-tamil24 5 ай бұрын
def letter_combinations(digits): digit_to_letters = { '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz' } if not digits: return [] result = [''] for digit in digits: letters = digit_to_letters.get(digit, '') result = [prefix + letter for prefix in result for letter in letters] return result # Example usage phone_number = "23" combinations = letter_combinations(phone_number) print(combinations) # Output: ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']
@gouravkhator
@gouravkhator 3 жыл бұрын
Time complexity is just 4^n. How come its n*4^n
@katja2207
@katja2207 3 жыл бұрын
same question here))
@kanyestan
@kanyestan 2 жыл бұрын
first n is the length of digits, the total of i in rec/backtrack function
@pranavingale6850
@pranavingale6850 8 ай бұрын
Damn I have a hard time understanding recursion stack, I mean how things will execute!
@danielsun716
@danielsun716 Жыл бұрын
def letterCombinations(self, digits: str) -> List[str]: if not digits: return [] phoneMap = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'} res, com = [], [] def backtrack(i): if len(com) == len(digits): res.append(''.join(com)) return for c in phoneMap[digits[i]]: com.append(c) backtrack(i + 1) com.pop() backtrack(0) return res
@rohithbhandari7836
@rohithbhandari7836 9 ай бұрын
Here we are modifying currStr why it is directly appended to output wy not its copy?
@yashshukla1637
@yashshukla1637 2 ай бұрын
In backtracking problems like **_Letter Combinations of a Phone Number_**, you can use two common approaches for building strings: --- #DIFF BW with pop and without pop ### **1. Using `pop()` (String Builder with List):** - **How it works**: You use a **list** as a mutable string builder. `append()` adds characters, and `pop()` removes them during backtracking. - **Efficiency**: - **Time Complexity**: `append()` and `pop()` are **O(1)**, making it faster for modifying the list. - **Memory**: Modifies the list in-place, reducing memory overhead. - **Backtracking**: After exploring one path, `pop()` removes the last letter, allowing the next possibility to be explored. ```python def backtrack(index, path): if len(path) == len(digits): combinations.append("".join(path)) return for letter in letters[digits[index]]: path.append(letter) backtrack(index + 1, path) path.pop() # Backtrack ``` --- ### **2. Not Using `pop()` (Direct String Concatenation):** - **How it works**: You pass a new **string** in each recursive call (`path + letter`), avoiding in-place modification. - **Efficiency**: - **Time Complexity**: String concatenation creates a new string each time, which is **O(n)** where `n` is the string length. - **Memory**: Each recursive call creates a new string, using more memory. - **Backtracking**: No need for `pop()` since you create a new string each time. ```python def backtrack(index, path): if len(path) == len(digits): combinations.append(path) return for letter in letters[digits[index]]: backtrack(index + 1, path + letter) ``` --- ### **Key Difference**: - **Using `pop()`**: Faster with **O(1)** list operations (_string builder_), better for performance. - **No `pop()`**: Easier to implement, but less efficient due to repeated string creation (**O(n)** for each concatenation). ---
@ajoydev8876
@ajoydev8876 2 жыл бұрын
But i don't understand why time complexity is O(n*4^n)..!
@Iamnoone56
@Iamnoone56 3 жыл бұрын
Neetcode bro i am not satisfied with backtracking playlist
@rentianxiang92
@rentianxiang92 3 жыл бұрын
Thank you as always!
@ankitakalangutkar7205
@ankitakalangutkar7205 3 жыл бұрын
time complexity is explained so well, thank you.
@veliea5160
@veliea5160 2 жыл бұрын
isn't supposed to be only 4^n. Why are we multiplying with n
@PippyPappyPatterson
@PippyPappyPatterson Жыл бұрын
@@veliea5160 bump
@SnapSHORT1
@SnapSHORT1 3 жыл бұрын
how can i think like this in a first place. I usually able to think only solution with iteration. how can I improve my problem solving with recursion .help please.
@mounirkanane8083
@mounirkanane8083 2 ай бұрын
Do a bunch of recursion problems, starting from easier ones to harder, even more basic than leetcode
@user-ys6ro4wi3f
@user-ys6ro4wi3f 8 ай бұрын
Using the digits input as a stack was a simpler solution for me, felt like cheating cus i skipped recursion lol letters = {'2': ['a','b','c'],'3': ['d','e','f'],'4': ['g','h','i'],'5': ['j','k','l'],'6': ['m','n','o'],'7': ['p','q','r','s'],'8': ['t','u','v'],'9': ['w','x','y','z']} digits = list(digits) if not digits: return [] res = letters[digits.pop()] while digits: last = letters[digits.pop()] newres = [] for i in range(len(res)): for j in range(len(last)): newres.append(last[j] + res[i]) res = newres return res
@ehabteima
@ehabteima Жыл бұрын
This is not a backtrack soln. It's just normal recursion approach.
@edwardteach2
@edwardteach2 3 жыл бұрын
U a God
@norax21
@norax21 Жыл бұрын
Can someone please explain why we should use backtrack(i+1,curStr+c) instead of return backtrack(i+1,curStr+c) in the recursive function?😭
@mohamadilhamramadhan6354
@mohamadilhamramadhan6354 Жыл бұрын
You can choose one of them. Both are valid solutions. For me uses backtrack(i+1, curStr+c) with res.append(curStr) is easier to think than build the combination from return backtrack().
@norax21
@norax21 Жыл бұрын
@@mohamadilhamramadhan6354 Thank you so much for the explanation!
@avadhut325
@avadhut325 2 жыл бұрын
We can write the function outside the first function. Why are you defining it inside another function? can anyone tell advantages and disadvantages of it?
@nguyenbach4710
@nguyenbach4710 Жыл бұрын
plz don't stop
@orangethemeow
@orangethemeow 2 жыл бұрын
In this problem, why do we not need to pop after running backtrack? I'm so confused at the .pop() in some backtrack problems, when should we use it?
@zerefX_
@zerefX_ 2 жыл бұрын
Using .pop() you are going to reduce your decision space, so you don't end up making up the same choice more than once
@ankitkataria7265
@ankitkataria7265 2 жыл бұрын
cause it's passed by value
@bufdud4
@bufdud4 2 жыл бұрын
The code builds a new string for every recursive call. On the other hand, when you pass in a list, that list is the same list for every recursive call.
@David-dm3po
@David-dm3po Жыл бұрын
We pop when our subset is an array we’re building and we want say 1,2 as one tree and 1,3 as another choice. We would need to pop the 2 from 1,2 so we can use the 1,3 combination. Then we continue with the rest of the numbers that same way. We’re using the same subset array so we have to pop to change the combination of numbers, or letters. Our combinations in this particular problem are made from letters in each property in the digitToChar object. So there’s no need to pop since the next digits’s characters contain the other choices we need to add to our string.
@noobCoder26
@noobCoder26 3 жыл бұрын
thanks sir
@symbol767
@symbol767 2 жыл бұрын
Thanks man
@yumindev
@yumindev 2 жыл бұрын
Hello, sir, what software do you use to draw on the whiteboard, well, black board ? Thanks.
@NeetCode
@NeetCode 2 жыл бұрын
I use Paint3D
@yumindev
@yumindev 2 жыл бұрын
@@NeetCode thankyou
@劉浩霆-c4j
@劉浩霆-c4j 6 ай бұрын
why can't we just use actual index of a list as digits instead of creating a map
@panmacabre9895
@panmacabre9895 3 жыл бұрын
Thank You
@supremoluminary
@supremoluminary Жыл бұрын
You went from a diagram explanation straight to the code. If you can't explain it simply, you don't really understand it yourself.
@julesrules1
@julesrules1 Жыл бұрын
I don't understand why the time complexity is N * 4^N yet. Can someone explain it?
@The6thProgrammer
@The6thProgrammer Жыл бұрын
At each of the 4^n solutions we copy a string of length n to a list which is a O(n) operation. So for all 4^n leaf nodes in the tree we do O(n) work which is n*4^n.
@CS_n00b
@CS_n00b Жыл бұрын
I don't see how this is backtracking as we are not doing any .pop()?
@GabrielGcbs
@GabrielGcbs 2 жыл бұрын
Nice video
@dhruvasoni6454
@dhruvasoni6454 2 жыл бұрын
try to code in c++ it will be great buddy
@harryz7973
@harryz7973 3 жыл бұрын
cool - what is the intuition behind - backtrack(i + 1, curStr + c) - ? The meat of this code.
@DanhWasHere
@DanhWasHere 3 жыл бұрын
He does it a couple times in his solutions: an inner function that iterates over the input and populates a list: he does this with DFS problems where the side effect of DFS is appending the popped vertex to the resulting list. backtrack's main function is to iterate over all the list of chars for a number and append to the result which will be returned
@anand8412
@anand8412 3 жыл бұрын
if you take his example 23 backtrack will be called as below: for each of the letter of first digit, it will be called with letter of second digit. SO base case will be covered when len(digit) == len(charStr) for example 2 is equal to 'ad' i.e in inner outer loop. (inner loop 1) backtrack(1,a) (inner outer loop 2,3,4) backtrack(2,ad) backtrack(2,ae) backtrack(2,af) (inner loop 2)backtrack(1,b) (inner outer loop 2,3,4) backtrack(2,bd) backtrack(2,be) backtrack(2,bf) (inner loop 3)backtrack(1,c) (inner outer loop 2,3,4) backtrack(2,cd) backtrack(2,ce) backtrack(2,cf)
@ilanaizelman3993
@ilanaizelman3993 3 жыл бұрын
Amazing.
@boddubharadwaj5923
@boddubharadwaj5923 3 жыл бұрын
Collections module makes it more effective .Am I right?
@jay-rathod-01
@jay-rathod-01 3 жыл бұрын
if u talking about java. then definitely...
@harshvardhanranvirsingh9473
@harshvardhanranvirsingh9473 3 жыл бұрын
I appreciate the video..but this is just a simple recursion that looks like backtracking..but this is not a backtracking.
@shreekarbukkapattanam3441
@shreekarbukkapattanam3441 3 жыл бұрын
yeah! ikr
@beginner6667
@beginner6667 10 ай бұрын
How that time complexity became n*4^n
@krateskim4169
@krateskim4169 2 жыл бұрын
super
@gokusaiyan1128
@gokusaiyan1128 2 жыл бұрын
which software do you use to write explanation/drawing ?
@veliea5160
@veliea5160 3 жыл бұрын
bro, I hope anything you touch turns to bitcoin for you!
@irisi3308
@irisi3308 3 жыл бұрын
What is the time complexity of the code?
@Ryanamahaffey
@Ryanamahaffey 2 жыл бұрын
Nein nein nein nein!
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 151 М.
N-Queens - Backtracking - Leetcode 51 - Python
17:51
NeetCode
Рет қаралды 180 М.
Yay😃 Let's make a Cute Handbag for me 👜 #diycrafts #shorts
00:33
LearnToon - Learn & Play
Рет қаралды 117 МЛН
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
Players vs Pitch 🤯
00:26
LE FOOT EN VIDÉO
Рет қаралды 138 МЛН
Backtracking: Permutations - Leetcode 46 - Python
9:43
NeetCode
Рет қаралды 365 М.
17 Letter Combinations of a Phone Number
32:20
Aditya Verma
Рет қаралды 4,6 М.
⚡️NEWS | RUBLE COLLAPSE | STRIKE ON CRIMEA | PUTIN IN KAZAKHSTAN
10:34
Ходорковский LIVE
Рет қаралды 172 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
Combinations - Leetcode 77 - Python
10:38
NeetCode
Рет қаралды 71 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 518 М.
What I Wish I Knew Before Becoming A Software Developer
15:06
Jeremiah Peoples
Рет қаралды 582 М.