Decode Ways

Sdílet
Vložit
  • čas přidán 5. 02. 2019
  • For business inquiries email partnerships@k2.codes My Desk Setup
    Desk - bit.ly/3jfY195
    Chair - amzn.to/2O9TM3r
    Monitor - amzn.to/3rcSHGa
    Webcam - amzn.to/2NUmwgi
    Desktop - amzn.to/3tiySPL
    Laptops - amzn.to/3aRoN3Z
    iPad - amzn.to/2LlJzzJ
    Keyboard - amzn.to/3jfbxdd
    Mouse - amzn.to/36ElWtT
    Wrist Rest - amzn.to/3trrHF4 (pls don't buy this)
    Mouse Pad - amzn.to/2Myz2lt
    Microphone - amzn.to/3atNyTA
    Lamp - amzn.to/3jjfZYp
    Headphones - amzn.to/3tvr0KU (new model)
    Headphone Hook - amzn.to/3tr8uTC
    Blue Light Glasses - amzn.to/3cDVUdK
    Wireless Charger - amzn.to/39LY1uu
    Keyboard cable - amzn.to/2O5p2R5
    Mic arm - amzn.to/3cECZj8
    Audio interface - amzn.to/36HdWIi
    Cloudlifter - amzn.to/36VO6kf
    Laptop dock - amzn.to/2O2DsBw
    Motherboard - amzn.to/3rkiWuA
    Solid state - amzn.to/3rk5vuo
    CPU cooler - amzn.to/3tnwwPA
    CableMod - amzn.to/3tqbtM8
    CPU - amzn.to/3auG1ns
    Power supply - amzn.to/3trsAxo
    RAM - amzn.to/39JZcuf
    Designing Data-Intensive Applications - amzn.to/2YK4ek1
    Clean Code - amzn.to/3txqfB5
    Meditations - amzn.to/3cDa4fi
    SOCIAL
    ----------------------------------------------------------------------------------------------------------------
    Support me on Patreon: / kevinnaughtonjr
    Follow me on Twitter: / kevinnaughtonjr
    Follow me on Instagram: / kevinnaught. .
    Follow me on GitHub: github.com/kdn251
    MUSIC
    ----------------------------------------------------------------------------------------------------------------
    best boy in ballincollig by jarjarjr
    / best-boy-in-ballincollig
    #coding #interviews #softwareengineering #dynamicprogramming Discord: bit.ly/K2-discord
  • Věda a technologie

Komentáře • 166

  • @KevinNaughtonJr
    @KevinNaughtonJr  Před 5 lety +33

    What kinds of videos can I make to help you guys?

    • @mayankdevnani894
      @mayankdevnani894 Před 5 lety +16

      (Suggestion)Maybe you could do this: Choose a topic and give an explanation on it(like how to approach it and what are the best ways to solve it with their corresponding complexities) and later solve one question on that particular topic(kind of hands-on tutorial).
      And yeah, please continue this coding series :)
      God bless you man :)

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +10

      @@mayankdevnani894 Awesome idea, I'll see what I can do with that :). I will continue these videos don't worry thank YOU for the support!!!

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

      what kinds of the problem need to use dp? because when I look at a problem I don't realize when should I use dp and how to use dp. I only know how to solve some dp problem bc I remember the way of solving them.

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

      Sort your video playlist according to easy, medium, hard.
      BTW, I loved your videos a d subscribed.

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

      Explaining recursion in depth including what happens in each stack frame and how not to lose track when debugging/tracking the calls. Also, I have noticed that each kind of recursive problem applied on Strings, Arrays or Trees have a recursive pattern. If you can help identify what patterns it boils down to or is an extension of.

  • @praveenchouhan6388
    @praveenchouhan6388 Před 4 lety +102

    @kevin, it would be really helpful if you first explain the approach and then start the implementation, that really helps us in getting the intuition behind the code.

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

      that's right. hard to follow without board explaination

    • @mrprime557
      @mrprime557 Před rokem

      @@divyadhaipullay5047 shutup dumb girl... like all girls u r dumb

  • @shrimatkapoor2200
    @shrimatkapoor2200 Před 3 lety +53

    Still can't wrap my head around why a string of length 0 has one way to decode it

    • @MonsterhunterFTWWTF
      @MonsterhunterFTWWTF Před 3 lety +9

      If there's nothing in the string, the only way to decode it is by not decoding it. Which counts as one way.

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

      @Surb Singh 0 decode into 0 ways. look at his ternary condition. If the single digit is a 0, assign dp[1] = 0 and return the last index of dp which is 0

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

      @@MonsterhunterFTWWTF Why 1, why not 0 (if you cannot decode it)

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

      @@ayusharora2019 I found this comment by Nikhil Sharma - see this
      why dp[0] = 1 :
      Back to basics - permutations and combinations:
      Combinations of len(n) is n!/r!(n-r)!
      (Even if r =0(min))
      n!/r!(n-r)! = 0!/0!*0! = 1

  • @whyisitnowhuh8691
    @whyisitnowhuh8691 Před 4 lety

    if this is using small problem to solve larger problem, could you use recursion as well? what would the solution using recursion look like?

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

    Hey thank you so much for this video, really helpful. I'm just having some trouble understanding why when you add to the twoDigits subproblem you add dp[i-2]. I get that it has to do with the fact that it's 2 digits, but if you could explain with other words that'd be great, thank you!

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

      For one digit, we go back 1 to get previous value and add it to dp[i]
      In case of two digits, we consider the last two digits as ONE value, so we go back 2 to get previous value and add to total sum of dp[i] which is already dp[i-1] currently. so basically we add twice for two digits (once for number of ways to decode considering two digits as two numbers and other for number of ways to decode of two digits as single number).
      if string is "126" dp[] = 1 1 2 3
      dp[0] = 1
      dp[1] = 1
      dp[2] = 2 -- initially 1 as we consider 1,2 for two letters( dp[1] ) and then add dp[0] as two digit is 12 so we can consider it as single letter
      dp[3] = 3 -- (2+1 same logic as dp[2])

  • @chintalapativenkataramarahul

    How come your oneDigit variable is taking the current character? s.substring( i-1 , i ) for i=2 will give the character at place 1, right? startIndex is inclusive and the end index is exclusive. Is my understanding wrong? How come your code is working?

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

    While updating dp[i] for single digit, we don't need to do dp[i] += dp[i-1] instead dp[i] = dp[i-1] works. Because dp[i] is already empty at this point.
    Great explanation Kevin. Thanks

    • @TulasiramV
      @TulasiramV Před 5 měsíci

      what gain are you getting from this optimization?

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

    dp[0] = 1 - How? I could not understand. The string is empty and it should be zero right?

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

    I didn’t understand why do we need to store the running total in an array to incur storage cost of O(N) Wouldn’t it be more efficient to have a local variable (say count or something) that maintains running total?

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

      Because he just parroted the answer from somewhere. He didn't explain anything. This explanation was: " I'm just typing this code and it works!"

    • @danavram8437
      @danavram8437 Před 4 lety

      The array is not there to act as the "running total", but as a cache. You are computing the new result based on the value of the old result. The array acts like a cache. Number of ways to decode string of length 3 is cache[2] (string of length 2) + cache[1] (string of length 1). Number of ways to decode string of length 4 is cache[3] + cache[2] and so on.
      You can use more variables, but the solution would probably look less clear.

    • @misoren9645
      @misoren9645 Před 3 lety

      @@dmitrysamoylenko6775 I agree, he barely goes into actual explanation. Like diagrams, drafts, intuition, walkthroughs.

  • @NirmalSilwal
    @NirmalSilwal Před 3 lety

    how did you come up with the intuition behind this problem

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

    Hey Kevin, What software do you use to create your videos?

  • @nikoinfanto729
    @nikoinfanto729 Před 4 lety

    What software do you use for the video overlay?

  • @Nikhilsharma-dp9tw
    @Nikhilsharma-dp9tw Před 4 lety +26

    And for all who worry why dp[0] = 1 :
    Back to basics - permutations and combinations:
    Combinations of len(n) is n!/r!(n-r)! (Even if r =0(min))
    = 0!/0!*0! = 1

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

      Then how da fk can dp[1] be 0 but not dp[0]

    • @rajatgupta-ld1wg
      @rajatgupta-ld1wg Před 3 lety +2

      @@bumq5514 now here dp[1] is dependent on first character of String and it's given in ques we don't have any mapping for '0', so its value will be 0 in that case. This string 0 and choosing nothing from nothing(0) are different.

  • @anirudhatalmale5575
    @anirudhatalmale5575 Před 4 lety

    Your explanations are THE BEST.
    Please go on posting more such solution videos with such great explanations

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

    thanks for the help! I realized that this is a fibonacci sequence, so no need for O(n) space

  • @ulti72
    @ulti72 Před 2 lety

    why dp[0] = 1, if there is no string number of ways should be 0 right?

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

    is it necessary to check for 0 at the start? The problem states that the array consists of a string of 'A'-'Z' encoded as number sequences. If you check for 0 at the start - which should be impossible - then wouldn't you also have to check for zero elsewhere, e.g. "2305" which would make 2 interpretations of the string invalid - 0 & 30. And then you'd have to indicate failure which isn't mentioned in the original problem description. Anyway, thanks again for another extremely well-explained solution

  • @ridhwaans
    @ridhwaans Před 5 lety

    what happens if the twoDigits have a leading zero? such as 02? The string 102 returns 3 but how does it represent 0 alphabetically

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

      102 can be decoded 1 way: 10, 2. You cannot decode it as 1, 02.

  • @iot3677
    @iot3677 Před 2 lety

    Thanks, I have watched a few videos for this problem. this one is nicely explained and easy to understand

  • @whitesalmon0925
    @whitesalmon0925 Před 4 lety

    much better explanation than those I found in the discussion section.

  • @ridhwaans
    @ridhwaans Před 5 lety +19

    I'm struggling to understand why dp[0] = 1; if the string is empty. If string has a length of 2, such as the number 10, it fetches dp[0] instead of its own merit

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

      There is always a way to decode an empty String and that is the only way hence we take it as 1.

    • @codearabawy
      @codearabawy Před 3 lety +7

      There will never be an empty string. The problem stated that the input is a non-empty string.

    • @pradeepparsam6471
      @pradeepparsam6471 Před 2 lety

      @@codearabawy What if the problem removes the restriction of the empty string then what will be dp[0]??

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

    How to know when to use bottom-up approach for a problem?

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

    For Facebook interviews, do you suggest doing the top Facebook questions or all questions tagged with Facebook? I'm actually unsure of the difference.
    As for videos, more videos with dynamic programming and recursion would be great!

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

      Thanks for the input Neal I'll try to add more videos with those topics. Hmmm it's funny because I've asked myself that before but I guess it's best to do both if possible...otherwise, I think if you choose Facebook from the company tags you'll be able to see questions within a certain time period like questions that have been asked in the last 6 months, 1 year etc if that's helpful?

    • @nealpatel7696
      @nealpatel7696 Před 5 lety

      @@KevinNaughtonJr would you prioritize the top ones if you could only do one list? I've done most of the top ones but want to get through the list without rushing it.

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

      @@nealpatel7696 I think either one is fine honestly (I think they're pretty similar in terms of questions?). Lmk if you have any other questions :)

  • @agenttickle6412
    @agenttickle6412 Před 3 lety

    I still had trouble understanding it. Used the debug tool in leetcode to go through it, and it made sense.

  • @mihirjoshi6742
    @mihirjoshi6742 Před 4 lety

    Thanks for explanation. I was stuck at this problem. But now I know how to do this question.

  • @chinmayswami9358
    @chinmayswami9358 Před 4 lety

    I came across a variation of this problem where a --> 0, b--> 1 and so on. How to work this variant ?
    ip:- 007 expected op = 1
    ip:- 200 expected op = 2
    ip = 100200300 expected op = 4
    Following seems to work:
    def numberOfSequences(input_string):
    dp = [0]*(len(input_string)+1)
    dp[0] = 1
    dp[1] = 1
    for i in range(2, len(input_string)+1):
    oneDig = int(input_string[i-1:i])
    twodig = int((input_string[i-2:i]))
    dp[i] += dp[i-1]
    if twodig >= 9 and twodig < 26 :
    dp[i] += dp[i-2]
    return dp[len(input_string)-1

  • @vivekpanchal8270
    @vivekpanchal8270 Před 4 lety

    Hello Kevin, great tutorial up there, but I am unable to pass "20" as testcase for above solution.

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

      Vivek Panchal if you’re having trouble with problems like this check out The Daily Byte! thedailybyte.dev/

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

    Not able to understand why we are doing dp[i] += dp[i-2]?? The way I understood the solution is that dp[0] is number of ways to decode input of length 0 and its value is 1. Same way dp[1] is number of ways to decode input of length 1 and its value is 1. So building on these base cases if we have 12 as input - we take either 1 or 2 and since its 1 digit we take value of dp[1] which is 1. moving on we consider both 12 which is a single entity but for this this why we are going back to dp[n-2] which is dp[0] which is number of ways to decode input of length 0? Please explain in layman terms if possible.

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

    Got it for Cisco SDE 2021 assessment

  • @vk1618
    @vk1618 Před 4 lety

    Dynamic programming where for lopp can be used - bottom-up problem

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

    I was trying to solve this problem without DP and my code is huge and macaroni level with all the if/else conditions...LOL... your code looks clean and simple, I like it. Thanks for sharing it! 👍

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety

      Thank you and anytime! Don't worry about it these questions are hard just keep practicing!!!

    • @amulyadubey3637
      @amulyadubey3637 Před 4 lety

      @@KevinNaughtonJr This code wont work for the case s="10"

    • @danavram8437
      @danavram8437 Před 4 lety

      @@amulyadubey3637 it does work on all cases, you might have a bug in your code.

  • @AS-xq3vw
    @AS-xq3vw Před 4 lety

    Thanks for explaining so well :)

  • @anthonycheng9822
    @anthonycheng9822 Před 5 lety +15

    Why is dp[0] = 1, shouldn't the ways to decode a string of length 0 be 0?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +20

      Hey Anthony, good question! I definitely found it confusing at first, but the way I like to think about it is there's no decoding necessary to decode a string of length 0...so the 1 way to decode it is doing nothing (i.e. the decoding of an empty string is an empty string). Hopefully that makes at least a little bit of sense? Otherwise for dp[0] = 1 in this problem and others I think about how the power set in math contains the empy set (sorry to bring in math). Not sure if they're great parallels but that's what my mind thinks of anyways. If anyone else has better explanations I'd love to hear them. I hope that helps Anthony!

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

      What I would say, if you don't initial it as 1. This code won't calculate properly for the twoDigits case . Thinking the input 12, it will return 1 but it should be 2.

    • @jayshekarharkar8119
      @jayshekarharkar8119 Před 4 lety

      @@xianfuzheng8250 Kevin's code does return 2 for input 12.

    • @zhengbob670
      @zhengbob670 Před 4 lety

      @@jayshekarharkar8119 Xianfu saying if dp[0] is not 1, 12 won't return 2. I'm also confusing why dp[0] is 1, according to dp[i] is the number of ways to decode a string of length i. dp[0] should not be 1. But Kevin gives a good example, sometimes we have to define some special cases, like in math 2^0 = 1. Thanks, Kevin.

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

      @@KevinNaughtonJr yeah the concept comes from theory of computation that even to accept an empty string we require 1 state in DFA,NFA.

  • @rchukkapalli1
    @rchukkapalli1 Před 4 lety

    How come dp[0] = 1; is isn't 0? string is empty and logically zero decoding. Can someone explain?

  • @fnumeherunnsa7491
    @fnumeherunnsa7491 Před 5 lety +29

    You are really great at explaining the problems. I wish you would do more Leetcode questions . Thank you :)

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

      Thank you so much for the kind words :) don't worry I'll keep the LeetCode videos coming and thank you so much for your support!!!

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

    Thanks for the video! If I see this on a real interview, would it be better to tell them the recursive way first and then explain the dp way?

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

      That's definitely a good idea! I think during interviews it's a good idea to offer multiple possible solutions and choose one after discussing that particular solutions benefits! Great question!

  • @jiaruizhu4578
    @jiaruizhu4578 Před 3 lety

    Love your explanation!

  • @sidn515
    @sidn515 Před 4 lety

    If there is no way to decode a string of length 0, shouldn't dp[0] = 0 instead of dp[0] = 1 ?

  • @cbjueueiwyru7472
    @cbjueueiwyru7472 Před 3 lety

    When he starts moving toward the submit button and jams start playing..... You know it's going to pass.

  • @milanpatel6240
    @milanpatel6240 Před 3 lety

    Thanks. It was so hard to understand in leetcode solution and you explained it easy way.

  • @rathna492
    @rathna492 Před 2 lety

    how is it dP[0] =1 for a string length of 0 there is no ways to decode , shouldnt it be 0?

  • @ahmadjawidmuhammadi9296

    I really like his voice ton when speaking. :)

  • @emilyhuang2759
    @emilyhuang2759 Před 2 lety

    I don't get why you check if the first character is "0". What happens if the "0" occurs somewhere else? Why only check at the index 0?

  • @Draco-pu4ro
    @Draco-pu4ro Před 3 lety +1

    What sort of questions/ problems can we expect for Data Engineers?

  • @l.e.nichols9382
    @l.e.nichols9382 Před 3 lety

    You have some wicked music taste!

  • @jigardoshi2852
    @jigardoshi2852 Před 3 lety

    Why is it that on line 10, its dp[i] += dp[i-1], wouldn't dp[i] = 0 and hence dp[i] should be = dp[i-1] if oneDigit >= 1 else 0

  • @judeankrah973
    @judeankrah973 Před 3 lety +7

    Those intros get me hyped. Great music selection.

  • @humeyratopcu3274
    @humeyratopcu3274 Před 4 lety

    Does anyone understand why dp[0]=1 ????

  • @chaitanyabharatdokara4978

    For 101
    the first digit is 1 second is 10 and again 1
    it gives me 2 possibilities. (Mistake at the conversion)

  • @ax5344
    @ax5344 Před 3 lety

    i have a hard time understanding 1) dp[0]=1, the speaker says there is one way to decode it, so 1. This does not quite make sense to me, as s="0", the answer should be 0, so "no way" to decode it. 2) i

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

    For twoDigit why we are doing dp[i]+= dp[i-2]??

    • @Paradise-kv7fn
      @Paradise-kv7fn Před 5 lety +2

      coz for twoDigit, we are considering i and i-1 which means that if we have XYZ and we are currently looking at YZ, then if YZ is valid, then it serves as one entity rather than two separate entities...Hence, we are going to add the number of ways to decode TILL "Y" which would be dp[i-2]

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

      we are finding two possible cases with every iteration,
      Case 1: single digit
      Case 2: two digit
      if single digit is valid, update dp[i]
      if two digit is also valid, update dp[i] but, dont forget case1 was also valid so we have to do dp[i] + dp[i-2].
      Hope it makes a little sense.

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

    The problem with this problem is to how to understand the problem not how to solve the problem .

  • @beverlyHillsAgent
    @beverlyHillsAgent Před 3 lety

    There is no way you can decode a string of length 0, so shouldn't it dp[0] be 0?

  • @himanshuchhikara4918
    @himanshuchhikara4918 Před 4 lety

    starting was lit

  • @mallikakejriwal3729
    @mallikakejriwal3729 Před 4 lety +17

    Came here looking for a solution , but instead, my heart melted seeing the way Kevin lighted up when his girl passes the camera

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

    Watched the video and read through some of the comments about dp[0] = 1...yeah still makes no sense lol. Yikes.

  • @parveenrajput292
    @parveenrajput292 Před 3 lety

    Kevin Naughton Jr. I think single variable is enough to handle result.

  • @gxo-mt5vo
    @gxo-mt5vo Před 11 měsíci

    If there is no way to decode an empty string, then shouldn't it return 0?

  • @vijendrakumarsharma5250

    nice one..!

  • @TheSmashten
    @TheSmashten Před 3 lety

    I can't figure out why this doesn't work in C++. Like, your same code is not even close to being right on C++. Ugh

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

    Ah man the DOOM in the inros and outros i s making this my fav channel

  • @aarchilohiya2408
    @aarchilohiya2408 Před 2 lety

    Possible to do it with stack

  • @jasonwang3690
    @jasonwang3690 Před 3 lety

    why is dp[0] = 1 ? you said there is no way to decode empty string, so no way = 1 ?

  • @daipayanhati2347
    @daipayanhati2347 Před 3 lety

    We do not need a DP array we could use the two pointer and one temp pointer for the calculation
    int count1 = 1;
    int count2 = 1;
    for(int i = 1;i0){
    count += count2;
    }
    if( dd>=10 && dd

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

      This works, but you also need to add the condition if(s[0] == '0') return 0; otherwise it fails for the input "0" as it will return count2=1

    • @daipayanhati2347
      @daipayanhati2347 Před 3 lety

      @@misoren9645 yeah thanks

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

    Can anyone please explain why we are doing :
    dp[i] += dp[i-1]; // In the first if statement.
    and
    dp[i] += dp[i-2]; //In the second if statement.
    Thanks.

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

    In python
    class Solution:
    def numDecodings(self, s: str) -> int:
    dp = [0]*(len(s)+1)
    dp[0] = 1
    dp[1] = 1 if s[0]!='0' else 0
    for i in range(2, len(s)+1):
    if 1

  • @trevthegamedev
    @trevthegamedev Před 4 lety

    Why does this problem have so many dislikes on leetcode?

  • @dpynsnyl
    @dpynsnyl Před 2 lety

    classic fibonacci series

  • @anilchaudhry804
    @anilchaudhry804 Před 5 lety

    Please try to make videos on questions from geeksforgeeks

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

    I don’t get why when the twoNumbers is valid we need to add the ways to decode the n-2 :(

    • @0anant0
      @0anant0 Před 4 lety

      For one digit, we go back 1 to get previous value.
      In case of two digits, we consider the last two digits as ONE value, so we go back 2 to get previous value.

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

    if dp[0] = 1, shouldn't then dp[1] = 2?

    • @0anant0
      @0anant0 Před 4 lety

      dp[1] = number of ways to decode a number at index 0, which can be number zero (invalid, since A maps to 1 and not zero, so dp[1] = 0, or 1 thru 9 -- a valid number, so dp[1]= 1.

  • @sachinkolachana6384
    @sachinkolachana6384 Před 2 lety

    If there is no way to decode an empty string, why would dp[0]=1 ?? dp[0] should be zero in that case.

  • @lifehacks9450
    @lifehacks9450 Před 4 lety

    Please upload some graph based questions

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

    Kevin, thanks a million for the video. It exemplifies how to approach a question on the interview so that you can fit within the time constraints - something still eludes me.
    It was a bit difficult to understand this it first and still found I had some gaps until I watched this video: czcams.com/video/5o-kdjv7FD0/video.html
    For someone who has not done DP problems, it is a great visualization.
    You did mention the stairs problem during your video and this is how I found the explanation I just posted above.
    This is not to criticize your video but just to help everyone in the forum understand the general approach better.
    What I got from both lessons is that Fibonacci is a simplified version of these more complex problems. Once you get that and the fact that you need to find a pattern for the base cases, the rest is easy :)

  • @mraryanrajawat9693
    @mraryanrajawat9693 Před rokem

    why dp[0]=1; ??

  • @jacksonleo3799
    @jacksonleo3799 Před 4 lety

    fib sequence with a condition

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

    how do u do these questions soo quickly lol...

  • @zaid4690
    @zaid4690 Před 3 lety

    I was asked this question by chase!

  • @ritwikrajpoot9751
    @ritwikrajpoot9751 Před 2 lety

    This guy jump to solution without discussing the approach.

  • @theanikaanisha4414
    @theanikaanisha4414 Před 4 lety

    function decodingWays(str){
    let res = []
    let obj = create() // a map of chars{1:'A'}...
    let startChar = str.charAt(0)
    if(obj[startChar]){
    res.push(1)
    }
    for(let i =1; i

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

    deserves more number of subscribers

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

    Video could have been more appealing if presenter can add some rationale to take 1-d dp array and what does it signify. It looked more like reading a leetcode solution. We don't want see solution, rather the rationale which isn't present in the video.

  • @jjc5258
    @jjc5258 Před 5 lety

    01 will return 0?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety

      Correct

    • @hanj5049
      @hanj5049 Před 4 lety

      Because let twoDigits = Number(s.slice(i-2, i)) for "01" will be 1, which will skip (twoDigits >= 10 && twoDigits

  • @anurag9110
    @anurag9110 Před 3 lety

    It's also better to walk through an example.

  • @bumq5514
    @bumq5514 Před 3 lety

    How in da fuk can dp[0] not be 0 but dp[1] can be 0

  • @alokcom
    @alokcom Před 3 lety

    Dp[0]= 0 ? there is no way

  • @ridhwaans
    @ridhwaans Před 5 lety

    the current solution doesnt work for '27'. it may not work for
    Input:
    "10"
    Output:
    2
    Expected:
    1
    Input:
    "12"
    Output:
    1
    Expected:
    2

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

    He didn't explain anything in this problem.

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

    Asked by : Amazon and Paytm too**

  • @pcccmn
    @pcccmn Před 2 lety

    I don't understand the logic "the number of ways to decode a string of length 0 is 1". How many ways can you decode an empty string? Zero. Why 1?

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

    Very Easy top down solution with Memoization
    public int numDecodings(String s) {
    if (s.length() < 1)
    return 0;
    int[] memo = new int[s.length() + 1];
    Arrays.fill(memo, Integer.MAX_VALUE);
    return numDecodings(s, 0, memo);
    }
    private int numDecodings(String s, int index, int[] memo) {
    if (index < s.length() && s.charAt(index) == '0') // check if it's zero
    return 0;
    if (index >= s.length() - 1)
    return 1;
    if (memo[index] != Integer.MAX_VALUE). // check if you already computer this value
    return memo[index];
    int total = numDecodings(s, index + 1, memo); // first case where you decode it as single
    int value = Integer.parseInt(s.substring(index, index + 2));
    boolean isDoubleDigit = (value > 9 && value < 27);
    if (isDoubleDigit)
    total += numDecodings(s, index + 2, memo); // second case where you decode it as double
    memo[index] = total; // store the values in the memo for quick usage
    return total;
    }

  • @developerabhishek7099
    @developerabhishek7099 Před 3 lety

    You should explain the approach first. Simply jumping to solution looks like you have memorized the code.

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

    Has anybody ever told you that you look so Indian?
    Nice explanation bdw :p

  • @pareshpandit802
    @pareshpandit802 Před 2 lety

    Why always bottom up? I would love to see more top down approaches..Thanks!!

  • @mrprime557
    @mrprime557 Před rokem

    bad bad badcow

  • @dibll
    @dibll Před 4 lety

    Not able to understand why we are doing dp[i] += dp[i-2]?? The way I understood the solution is that dp[0] is number of ways to decode input of length 0 and its value is 1. Same way dp[1] is number of ways to decode input of length 1 and its value is 1. So building on these base cases if we have 12 as input - we take either 1 or 2 and since its 1 digit we take value of dp[1] which is 1. moving on we consider both 12 which is a single entity but for this this why we are going back to dp[n-2] which is dp[0] which is number of ways to decode input of length 0? Please explain in layman terms if possible.