Total Ways To Decode A String - Recursive Dynamic Programming Approach ("Decode Ways" on LeetCode)

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • Free 5-Day Mini-Course: backtobackswe.com
    Try Our Full Platform: backtobackswe.com/pricing
    📹 Intuitive Video Explanations
    🏃 Run Code As You Learn
    💾 Save Progress
    ❓New Unseen Questions
    🔎 Get All Solutions
    Question: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - 1 'B' - 2 ... 'Z' - 26. Given a non-empty string containing only digits, determine the total number of ways to decode it.
    Examples:
    1
    Input: "12"
    Output: 2
    Explanation: It could be decoded as "AB" (1 2) or "L" (12).
    2
    Input: "226"
    Output: 3
    Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
    Complexities:
    n is the total digits in the input string
    Time: O( n )
    Memoization prunes our recursion tree and we will do a linear amount of work to solve the problem.
    Space: O( n )
    We will need to store the answer to up to n subproblems that we will need to calculate
    ++++++++++++++++++++++++++++++++++++++++++++++++++
    HackerRank: / @hackerrankofficial
    Tuschar Roy: / tusharroy2525
    GeeksForGeeks: / @geeksforgeeksvideos
    Jarvis Johnson: / vsympathyv
    Success In Tech: / @successintech
  • Věda a technologie

Komentáře • 263

  • @BackToBackSWE
    @BackToBackSWE  Před 5 lety +22

    Table of Contents:
    Messing Around 0:00 - 1:16
    The Problem Introduction 1:16 - 2:54
    Recursion Tree Walkthrough 2:54 - 8:06
    Pruning The Recursion (With Memoization) 8:06 - 10:26
    Time Complexity 10:26 - 10:40
    Space Complexity 10:40 - 11:22
    Yeah 11:22 - 11:38
    The solution for this problem as described in the video is in the description.

  • @cemtorun3815
    @cemtorun3815 Před 5 lety +48

    Hey man, I just really love how you teach the concepts and not the coding. We can find the code in whatever language on LC but this is actually really helping me understand how to get to the answer - keep up the great work! :)

  • @rahul-patil
    @rahul-patil Před 3 lety +7

    The recursion code is not in the description. :(
    Please provide the link.
    Thanks

  • @colorfulcodes
    @colorfulcodes Před 5 lety +100

    lollllll, the intro.

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

    Your way of explaining first the problem with the O(2n) approach then going to the optimised one really helps a lot. Thanks for your rich videos. :)

  • @axchetype6206
    @axchetype6206 Před 5 lety +12

    Dude your intros are the best lol. Thanks a lot for your videos!

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety

      I know. I'm just messing around since these videos are generally boring to watch.

  • @frankchen9264
    @frankchen9264 Před 5 lety +17

    I had a facebook phone interview last week. It's exactly this question!!!!! But I couldn't solve it at that time. I felt really depressed. Thanks for this video! I become more clear about DFS.

  • @aj9706
    @aj9706 Před 4 lety +7

    The way u explain the logic using recursion tree is superb.... Thank you

  • @spk9434
    @spk9434 Před 5 lety +8

    Exactly what I was looking for ... a recursive solution. Thanks man.

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety

      you're welcome, the code is in the description if you haven't already found it

    • @tilak1233
      @tilak1233 Před 3 lety

      @@BackToBackSWE where ?

  • @RahulVerma-fz2jf
    @RahulVerma-fz2jf Před 4 lety +6

    Really Really good man. Understood the solution in depth. The Recursion tree helped a lot. Keep up the good work.

  • @jeffmathew5857
    @jeffmathew5857 Před 5 lety +4

    You have a great knack for explaining these problems. Fantastic Work!

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety +2

      im trying

    • @jeffmathew5857
      @jeffmathew5857 Před 3 lety

      @@BackToBackSWE Came back to this problem and video again after 2 years. Just saw your reply. You're still nailing it in 2021 :)

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

    when you just dropped the time complexity from exponential to linear by pruning the tree .. BRAINS OUT !!
    Amazing, really helpful!

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

    this is my first video on your channel , i dont know dp , currently i am learning recursion only , man your explanation is outstanding! I love it ,, thank you

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

    I've seen a few videos until now, and I can say your explanations are always better than others😊Thank you always. And I also love your clear voices!

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

    I am just wondering there code is ? I didn't find anything in the description

  • @ducvu8310
    @ducvu8310 Před 4 lety +4

    Loved your introduction, :) your explanation is great as always...

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

    I like how you explain the problem/solution without using code in the video. It lets me understand what needs to be done without giving me a bias on how to code a solution when I re-attempt the problem.

  • @aneeshsagiraju3985
    @aneeshsagiraju3985 Před 3 lety

    i was using backing tracking for this and got TLE, storing the solution is great idea to reduce time complexity . Thanks

  • @meghnaverma1809
    @meghnaverma1809 Před 4 lety +16

    Well i must say that you explained the concept of dynamic programming deeply in this video and actually made me realize that how to think from the basic . Thanks !

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

    Thanks to show me the recursion approach of this problem. If I had skipped to DP solution , I would never understand the reason behind this problem. Recursion ,then memoization and then DP (Bottom UP approach) is the best sequence. DP is hard and your videos is really making me understand DP. I know that I need practice, practice and practice.

  • @thomaspinella8156
    @thomaspinella8156 Před 5 lety +1

    Thank you so much for the videos!

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety

      It is nothing. Just keep learning and be the best you can be. That is why I am here.

  • @adityasoni1207
    @adityasoni1207 Před rokem

    It's Aug, 2022 and I am still loving these videos! Amazing work! 🙏

  • @AW-rt9sz
    @AW-rt9sz Před 4 lety +4

    Shit, I came up with recursive solution by myself but without tree pruning, realized it was too slow. Then I found your video, thank you for teaching me a valuable lesson!!!!!!!!!

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

    Hey! Great explanation but where's the code for this question??? I can't find it in the description.

  • @sunnyding602
    @sunnyding602 Před 5 lety +2

    Amazing clean at easy to understand code. I also like the intro.

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

    Beautifully explained .Thanks much

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

    Hi Ben, I know you mentioned "Space Complexity @10:20 and @10:40 - 11:22." But can you explain why we use Space Complexity - O(n) on the call stack - in the brute force approach?

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

      If we decompose 1 char at a time it is string length in call stack frames created

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

    Say I want to store all the possible decodings in a string array.
    can we still do it in O(n) time? if so, what do I need to do to store the
    decodings in a recursive approach?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      Can you elaborate on the approach you are thinking of?

  • @chandanhegde4923
    @chandanhegde4923 Před 5 lety +5

    Very nice video.
    So, as per the code, the final answer will be returned by previousAnswers[0] right?
    Also is there any solution out there that has just recursion and doesn't use DP? Would be of great help. I couldn't find any. I know that it would be inefficient, but wanted to first learn that and then jump into DP problems.

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety +1

      Yeah, and if it didn't cache answers nothing would change except the time complexity would simply become worse. All caching does is prune the recursion tree.

    • @chandanhegde4923
      @chandanhegde4923 Před 5 lety +1

      @@BackToBackSWE thanks for the clarification.
      Basically
      arr[i] = arr[i+1] (+ arr[i+2] if 2 chars are valid)
      Also was wondering if the question was to print all the possible decoded ways instead of just the count, would it be possible to solve it in under O(n^2) ? I tried, an iterative solution using a
      map and char append strategy, but couldn't optimize it.

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety

      @@chandanhegde4923 I'm not sure, I'd have to think about this.

  • @imj5296
    @imj5296 Před 2 lety

    I love your channel a lot!

  • @ak_allday__
    @ak_allday__ Před 4 lety +4

    Hey Benyam, I'm not able to figure out the subproblems for new DP questions. Do I just practice more and more problems to see a pattern amongst these problems? Because I can't afford to loose time figuring out these sub problems in a coding challenge with limited time...

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      Yeah, you just have to do many problems and read textbooks. Which makes dp problems stupid.

    • @ak_allday__
      @ak_allday__ Před 4 lety +1

      Thanks! That really helped

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      @@ak_allday__ Did it? I didn't say much.

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

      Back To Back SWE yes you did. Instead of being anxious not able to think of sub problems, I realize if I patiently keep pushing myself, soon I will be able solve these problems easily...

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

    What an introduction!!

  • @fredwooten14
    @fredwooten14 Před 5 lety +2

    Man took a launch code assessment test kick my butt.

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

    bro you're a beast. subscribed!

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

    Hey , I am new to DP.
    What I thought is to recurse all the digits at each node, But I didn't get that we can make choice that we can pick either one element / two element.
    Need some help regarding how did you arrive to that decision.
    .
    Thank u SO much. Your videos are awesome.

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      Thanks and the idea is we follow the "paths" of decomposition. Path items can only be single or 2 digit numbers.

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

    Where is the solution in the description?

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

    Recursion and algorithm is clear but I don't understand how to implement dynamic programming in it? Any suggestions?

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

      Got it and the DP part is just caching the answes to substrings in a 1D array

  • @gameplayer1980
    @gameplayer1980 Před 4 lety +1

    Loved that opening zinger.

  • @password47403
    @password47403 Před 2 lety

    This explanation is one of the most intuitive explanations i have found on the internet...KUDOS!!!

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

    Can you please share the code?

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

    The best explanation I have heard ...thank you 😊

  • @fredwooten14
    @fredwooten14 Před 5 lety +3

    Question my brother! Is perfecting interview questions the goal?

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

    Great explanation, only advice would be to go in a bit more explanation into what the memo-schema looks like. Are we storing the index at a certain point as the key and the value is the number of ways you can decode?

  • @parthsohani2429
    @parthsohani2429 Před 4 lety +1

    Where can i find the code(your code) related to this?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated and we only maintain backtobackswe.com now.

  • @vinayprakash1687
    @vinayprakash1687 Před 5 lety +2

    Is there a way to do it in a bottom-up approach instead of top down?

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety +1

      Yeah, Leetcode has a lot of them just iteratively going through the string

  • @blackmountain2884
    @blackmountain2884 Před rokem

    Great video! Having trouble finding the iterative solution video

  • @baohezhang112
    @baohezhang112 Před 5 lety +1

    Thank you!

  • @aditabangard
    @aditabangard Před 4 lety +1

    the github link doesnt exist. Code is missing. but I like the conceptualization of the problem. facing issue with pruning the tree

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

    just wow.. keep it going buddy

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

    my soul lights up when he says "expression"

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

    cant find the solution in description... can you please check ?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated - we only maintain backtobackswe.com now.

  • @PankajKP
    @PankajKP Před 5 lety +2

    isnt it similar to ip decomposition.. Can we apply memoization to ip decomposition too??????

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety

      sort of? For ip decomposition we use pruned recursion to generate all possible decompositions (I'm not sure how we could use dynamic programming for that). For this problem we do recursion as well, pruning our recursion tree along the way down.

  • @ankitbhardwaj9566
    @ankitbhardwaj9566 Před 3 lety

    Where is the code bro in description?

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

    but where's the code?

  • @linhdantran1859
    @linhdantran1859 Před 5 lety +2

    Very good explanation.

  • @NJTROJAN
    @NJTROJAN Před 5 lety +1

    In your solution, "int[] previousAnswers = new int[s.length() + 1];" you don't really need a dp array size s.length() + 1, your answer is at previousAnswers[0] and the length of previousAnswers[] should be s.length(). Love your videos, they are very helpful! :)

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety

      Are you sure? Been a while since I wrote the same, I did it hastily. If you are correct and the fix passes test cases, if you open a PR I can merge it in.

    • @NJTROJAN
      @NJTROJAN Před 5 lety +1

      @@BackToBackSWE Yes i have verified it. Created a PR for you.

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety

      @@NJTROJAN ok

  • @rahgurung
    @rahgurung Před 4 lety +1

    But where is the code in description?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated, we only maintain backtobackswe.com now.

  • @srinimurthy
    @srinimurthy Před 3 lety

    I am confused.. Take string "2263"
    I can have 2, 22, 26 and 3, can't I. So why is the answer 3 ?

  • @seungjinkim8860
    @seungjinkim8860 Před 4 lety +1

    Thanks for the video! Where can I find the code?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      repository is deprecated - we only maintain code on backtobackswe.com now - it is what we are building

  • @soumyadeb7358
    @soumyadeb7358 Před 3 lety

    The intro made my day! xD

  • @cibureh2684
    @cibureh2684 Před 3 lety

    You are the best!

  • @galaxyntfr6256
    @galaxyntfr6256 Před 5 lety +2

    This is good content please do "expression-add-operators" next

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety

      Added it to the first 100 video list. That question is very good. It looks like a Facebook question I saw a while back. Looks to me like backtracking but I'll see when I do my notes for it.

  • @amankumarkashyap400
    @amankumarkashyap400 Před 5 lety +3

    What will be the iterative solution to this problem

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety +1

      leetcode.com/problems/decode-ways/discuss/30358/Java-clean-DP-solution-with-explanation

  • @anshulsaxena4899
    @anshulsaxena4899 Před 4 lety +1

    Where is the link to code? I can't find it.

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated - we only maintain backtobackswe.com now.

  • @MartinThoma
    @MartinThoma Před 4 lety +5

    Although the recursive solution is straight-forward, it can only cope with strings of length ~500 in Python. Once it gets longer, there will be a RecursionError.

  • @sirasanihemaspandana7390

    Super explanation 👍👍

  • @PeterLitvak
    @PeterLitvak Před 3 lety

    So where is the example of memorization code?

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

    Where is the code you said in the description????

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

    There is no link to the code in your description, please add it. In your video if you explain with the code by dry run then the intuition makes more sense

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated - we only maintain backtobackswe.com now.

  • @spk9434
    @spk9434 Před 5 lety

    This is string decomposition, ofcourse with some pruning. right ?

    • @BackToBackSWE
      @BackToBackSWE  Před 5 lety +1

      Yes. The decomposition only follows a valid path that a proper decoding can follow. Correct.

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

    I don't see any code in the description

  • @ptehack448
    @ptehack448 Před 4 lety +1

    Can you please paste an alternate link for the code ? It says page not found

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      yeah it is broken

    • @ptehack448
      @ptehack448 Před 4 lety

      @@BackToBackSWE could you send the code to me please. ?
      I'm preparing for interviews and i really need it !

    • @ptehack448
      @ptehack448 Před 4 lety

      mrvaibhavrastogi@hotmail.com

  • @malkarajk4093
    @malkarajk4093 Před 4 lety +1

    actually where is the code ??
    i didn't found it

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated - we only maintain backtobackswe.com now.

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

    What is the determination of a character is decodable? What is the rule ?

  • @yark618
    @yark618 Před 4 lety +1

    Great vid

  • @Steelmax6
    @Steelmax6 Před 5 lety +2

    Haha that's gonna be me next Tuesday

  • @CHIRANJIBNANDY1
    @CHIRANJIBNANDY1 Před 4 lety +1

    Code link is broken. Please fix the same.

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

    where is the code of this problem?

    • @BackToBackSWE
      @BackToBackSWE  Před 3 lety

      The repository is deprecated - we only maintain backtobackswe.com now.

  • @ravindraverma8413
    @ravindraverma8413 Před 4 lety +1

    where is code, u say its in description ?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated - we only maintain backtobackswe.com.

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

    i've following u since months now. i really want to buy you subscription, but i m not that sound financially... is there any discount i can get ??
    u r great really.......

  • @rebeccacarroll8385
    @rebeccacarroll8385 Před 4 lety +1

    github link doesn't work :-(

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

    why did you stopped uploading videos dude!!!.your videos are really helpful

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

      we are undergoing organizational changes and I'm at a life transition point

    • @Kushagra_21
      @Kushagra_21 Před 3 lety

      @@BackToBackSWE well hope ur transition is smooth and as u say u will make this channel one of the largest resource for software engineering interviews!!

  • @energyandt9004
    @energyandt9004 Před 3 lety

    This intro is fire bruh lol

  • @sakshamgupta298
    @sakshamgupta298 Před 3 lety

    cant find the solution

  • @ashishchoksi8501
    @ashishchoksi8501 Před 4 lety +1

    why you remove code link ????

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated - we only maintain backtobackswe.com

  • @rohitnegi4651
    @rohitnegi4651 Před 4 lety +1

    I don't see code in description.. can u post it?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated - we only maintain backtobackswe.com now.

    • @rohitnegi4651
      @rohitnegi4651 Před 4 lety

      @@BackToBackSWE oh okay👍

  • @tirthaghoshdastidar9668
    @tirthaghoshdastidar9668 Před 4 lety +1

    Git link seems to be broken

  • @Tlacoyo59a
    @Tlacoyo59a Před 4 lety +1

    The code link is broken

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

    Code is missing

  • @demidrek-heyward
    @demidrek-heyward Před 4 lety +1

    thanks@

  • @priyankajojan6143
    @priyankajojan6143 Před 4 lety +1

    What will be the answer for "01"? And How can you please explain?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety +1

      I believe 1, I did this a long time ago, etc.

    • @rachitsaksena232
      @rachitsaksena232 Před 4 lety +1

      It will be 0. We cannot decode this string. Since in order for a string to have zero, it must be preceded by a 1 or a 2.( 10 and 20 are the only valid codes possible)

    • @priyankajojan6143
      @priyankajojan6143 Před 4 lety

      @@rachitsaksena232 Thanks!

  • @thomaslee3621
    @thomaslee3621 Před 3 lety

    Where is the solution?

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

    Hi is there an implementation in Python?

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

      We only maintain code samples on backtobackswe.com (paywall) now.

    • @Whiteroca
      @Whiteroca Před 3 lety

      ​@@BackToBackSWE Thank you, will keep in mind :)

  • @killerdwag
    @killerdwag Před 8 měsíci

    There's no solution in the description?

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

      Happy Holidays 🎉 You can find the code on our platform andmany other questions! We'd love to offer you a 40% Off our exclusive lifetime membership just use the code CHEER40 - backtobackswe.com/checkout?plan=lifetime-legacy&discount_code=CHEER40

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

    where's the link to the code?

    • @BackToBackSWE
      @BackToBackSWE  Před 3 lety

      The repository is deprecated - we only maintain backtobackswe.com now.

    • @deepsonshrestha5646
      @deepsonshrestha5646 Před 3 lety

      @@BackToBackSWE meaning I need to buy the product ? XD

  • @jaskaransethi2000
    @jaskaransethi2000 Před 4 lety +1

    Can you do 301. Remove Invalid parentheses please?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety +1

      hey, I would but busy with school and other things

    • @jaskaransethi2000
      @jaskaransethi2000 Před 4 lety

      @@BackToBackSWE If you'd get time, please make one. Thanks

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      @@jaskaransethi2000 I don't have the time I think

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

    Where the hell is the code then?

  • @goutamkreddy
    @goutamkreddy Před 4 lety +1

    github link does not work!

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

    where's the code?????

    • @BackToBackSWE
      @BackToBackSWE  Před 3 lety

      The repository is deprecated - we only maintain backtobackswe.com now.

  • @cmbubai4671
    @cmbubai4671 Před 4 lety +1

    where is code?

    • @BackToBackSWE
      @BackToBackSWE  Před 4 lety

      The repository is deprecated - we only maintain backtobackswe.com now.