Letter Combinations of a Phone Number - Backtracking - Leetcode 17

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

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

  • @NeetCode
    @NeetCode  3 года назад +13

    🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤

  • @themagickalmagickman
    @themagickalmagickman Год назад +61

    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 11 месяцев назад +6

      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 10 месяцев назад

      @@fredtrentini6791 i thought i was the only one hustling this long with 30 lines of code writing.

    • @tonyiommisg
      @tonyiommisg 8 месяцев назад

      @@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 3 месяца назад

      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 2 месяца назад +1

      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.

  • @jay-rathod-01
    @jay-rathod-01 3 года назад +68

    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 года назад +3

      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] ruclips.net/video/qAs9laxC-rs/видео.html

  • @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.

  • @PAIPENG-c2b
    @PAIPENG-c2b 10 месяцев назад +9

    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

  • @The6thProgrammer
    @The6thProgrammer Год назад +2

    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 6 месяцев назад

      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.

  • @jiwachhetri4165
    @jiwachhetri4165 2 года назад +11

    His explanation makes it so easy to understand even for beginners

  • @baap6886
    @baap6886 2 года назад +8

    best coding channel for DSA and best approaches.
    Love From India❤

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

    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 года назад +5

    Similar idea and coding as #78 Subset but more easier. Thanks so much for the explanations!

  • @zehuazhou3390
    @zehuazhou3390 2 года назад +2

    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 2 года назад +1

      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

  • @rajeshmadira8306
    @rajeshmadira8306 3 года назад +7

    Excellent explanation, Please try to explain more problems per week

  • @jkk23-g7c
    @jkk23-g7c 3 месяца назад

    I somehow coded the solution myself, but wasn't sure why it worked. You have the best explanations

  • @theraczcar
    @theraczcar 3 года назад +7

    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 года назад +1

      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 😢

  • @Chansd5
    @Chansd5 2 года назад +8

    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 года назад +4

      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.

  • @IsomerMashups
    @IsomerMashups 3 года назад +7

    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.

  • @albertlee5648
    @albertlee5648 Год назад +1

    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

  • @AnnieBox
    @AnnieBox 2 года назад +5

    backtracking is always my painpoint but everything looks so neat and easy to understand from you video!👍🏻

  • @09sangram
    @09sangram Год назад

    this the only tech foreign channel I follow from India. Good going

  • @shreekarbukkapattanam3441
    @shreekarbukkapattanam3441 3 года назад +39

    great approach but is this backtracking!!?? i think its recursive only

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

      Correct, still a nice approach

    • @Pranav-nn1lu
      @Pranav-nn1lu 2 года назад +8

      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 Год назад +2

      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 5 месяцев назад

      ​@@The6thProgrammercorrect

    • @Dhruvbala
      @Dhruvbala 4 месяца назад

      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

  • @yashshukla1637
    @yashshukla1637 Месяц назад

    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).
    ---

  • @Thisismyworld123
    @Thisismyworld123 2 года назад +13

    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!

  • @illu1na
    @illu1na Год назад +1

    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

  • @umberto3271
    @umberto3271 2 года назад +5

    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 Год назад +3

      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.

  • @alexisacosta6758
    @alexisacosta6758 4 месяца назад +1

    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 3 месяца назад

      correct

    • @yimengjiang9614
      @yimengjiang9614 Месяц назад

      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.

  • @Fanaro
    @Fanaro 7 месяцев назад

    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 года назад +3

    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 года назад +1

      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.

  • @luanlucas8605
    @luanlucas8605 3 года назад +5

    Great explanation, please keep it up! :)

  • @akhileshb5859
    @akhileshb5859 2 года назад +5

    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 Год назад +1

      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!

  • @akashverma5756
    @akashverma5756 Год назад

    Earlier, Backtracking was nightmare for me. Now, It is easier than any algorithm I have learned till now.

  • @sharoncohen318
    @sharoncohen318 Год назад +2

    This solution beats 19%, I did this iteratively and it beat 96%.

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

    First thing I thought about is a graph problem with an Inorder traversal

  • @EddieCheng174
    @EddieCheng174 Год назад +1

    I am curious where is the backtracking part of this question 🤔

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

    So neat, this deserves a lot more likes

  • @ravipatel-xu5qi
    @ravipatel-xu5qi Год назад +1

    I would suggest that you should have explained this with
    input "234"

  • @nazarzimarev8657
    @nazarzimarev8657 7 месяцев назад

    Amazing.... Didnt think that we can use recursion

  • @zackgrey4472
    @zackgrey4472 Год назад

    I'm finally getting backtracking algorithms in < 20m :-.) maybe I'll actually be able to get a job, tysm Leetcode king

  • @LotusTree-jw1mr
    @LotusTree-jw1mr 2 года назад +1

    Great video! Can I please ask how did you make a video like this? What tools did you use? thank you!

  • @joshgung
    @joshgung Год назад

    This question is asked today to me in amazon interview :) Could not answer :D

  • @NinjiaJoeLife
    @NinjiaJoeLife 2 года назад +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.

  • @AbanoubAsaad-YT
    @AbanoubAsaad-YT 2 года назад

    Thank you for the explanation/solution!
    I always forgot drawing the decision tree :(

  • @raychang6443
    @raychang6443 2 года назад +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 года назад +1

      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)

  • @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

  • @freeadvice-tamil24
    @freeadvice-tamil24 4 месяца назад

    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']

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

    I was asked this question at Facebook interview.

  • @abielkim960
    @abielkim960 2 года назад +5

    can someone please explain why the time complexity is O(n*4^n)? and not O(4^n)? Thank you.

    • @kyayaarbankey
      @kyayaarbankey 2 года назад +3

      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 года назад +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.

  • @user-ys6ro4wi3f
    @user-ys6ro4wi3f 7 месяцев назад

    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

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

    Thank you as always!

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

    I like your channel and voice as well, lol...

  • @BobBob-e
    @BobBob-e Год назад +1

    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.

  • @rohithbhandari7836
    @rohithbhandari7836 8 месяцев назад

    Here we are modifying currStr why it is directly appended to output wy not its copy?

  • @gouravkhator
    @gouravkhator 3 года назад +3

    Time complexity is just 4^n. How come its n*4^n

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

      same question here))

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

      first n is the length of digits, the total of i in rec/backtrack function

  • @yilmazbingol4838
    @yilmazbingol4838 3 года назад +2

    if not digits:
    return []
    write this at the beginning of algorithm, you will get much better result

  • @danielsun716
    @danielsun716 Год назад +1

    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

  • @pranavingale6850
    @pranavingale6850 7 месяцев назад +1

    Damn I have a hard time understanding recursion stack, I mean how things will execute!

  • @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?

  • @ajoydev8876
    @ajoydev8876 2 года назад +1

    But i don't understand why time complexity is O(n*4^n)..!

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

    thanks sir

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

    Thank You

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

    Thanks man

  • @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 года назад +1

      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 года назад +3

      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 Год назад +1

      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.

  • @Iamnoone56
    @Iamnoone56 2 года назад +1

    Neetcode bro i am not satisfied with backtracking playlist

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

    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 29 дней назад

      Do a bunch of recursion problems, starting from easier ones to harder, even more basic than leetcode

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

    U a God

  • @劉浩霆-c4j
    @劉浩霆-c4j 5 месяцев назад

    why can't we just use actual index of a list as digits instead of creating a map

  • @CS_n00b
    @CS_n00b Год назад

    I don't see how this is backtracking as we are not doing any .pop()?

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

    Hello, sir, what software do you use to draw on the whiteboard, well, black board ? Thanks.

    • @NeetCode
      @NeetCode  2 года назад +2

      I use Paint3D

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

      @@NeetCode thankyou

  • @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.

  • @ehabteima
    @ehabteima Год назад

    This is not a backtrack soln. It's just normal recursion approach.

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

    try to code in c++ it will be great buddy

  • @harryz7973
    @harryz7973 3 года назад +2

    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 2 года назад

      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)

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

    Nice video

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

    which software do you use to write explanation/drawing ?

  • @nguyenbach4710
    @nguyenbach4710 Год назад

    plz don't stop

  • @Jun-zq3bn
    @Jun-zq3bn 6 дней назад

    should be "pqrs" for 7 instead of "qprs"

  • @ankitakalangutkar7205
    @ankitakalangutkar7205 3 года назад +2

    time complexity is explained so well, thank you.

  • @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...

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

    Amazing.

  • @harshvardhanranvirsingh9473
    @harshvardhanranvirsingh9473 3 года назад +5

    I appreciate the video..but this is just a simple recursion that looks like backtracking..but this is not a backtracking.

  • @supremoluminary
    @supremoluminary Год назад +1

    You went from a diagram explanation straight to the code.
    If you can't explain it simply, you don't really understand it yourself.

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

    super

  • @beginner6667
    @beginner6667 9 месяцев назад

    How that time complexity became n*4^n

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

    What is the time complexity of the code?

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

    bro, I hope anything you touch turns to bitcoin for you!

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

    Nein nein nein nein!