Decode Ways | Recursion | Memo | Bottom Up | Leetcode 91

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024
  • Whatsapp Community Link : www.whatsapp.c...
    iPad PDF Notes - github.com/MAZ...
    This is the 75th Video of our Playlist "Dynamic Programming : Popular Interview Problems".
    In this video we will try to solve a famous and good DP Problem - Decode Ways (Leetcode 91).
    I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Also, please note that my Github solution link below contains both C++ as well as JAVA code.
    Problem Name : Decode Ways
    Company Tags : Google, Meta, Microsoft, Uber
    My solutions on Github(C++ & JAVA) : github.com/MAZ...
    Leetcode Link : leetcode.com/p...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My GitHub Repo for interview preparation : github.com/MAZ...
    Subscribe to my channel : / @codestorywithmik
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Approach-1 Summary (Recursion + Memoization) : The solve function explores decoding possibilities, memoizing results to avoid redundant calculations. The approach considers base cases for index bounds and '0' digits, and recursively calculates decoding possibilities for single and double-digit combinations. The method initializes memoization, sets the string length, and returns the total number of decoding possibilities.
    Approach-1 Summary (Bottom Up) : In this approach, we iterate through the string, maintaining a vector t to store the number of ways to decode strings of various lengths. It updates the decoding possibilities based on the current and previous digits, considering valid two-digit combinations. The method initializes the string length n and returns the total number of decoding possibilities.
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    ✨ Timelines✨
    00:00 - Introduction
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa

ความคิดเห็น • 73

  • @DevOpskagyaan
    @DevOpskagyaan 9 หลายเดือนก่อน +14

    Give this guy an Award 🫡❤️
    Hats off to such a detailed explanation 🙌

  • @ManishGupta-lz8ho
    @ManishGupta-lz8ho 9 หลายเดือนก่อน +3

    yr aap addictive ho question khud se ho bhi jaaye phir your solution really kuch na kuch extra samjha hi jaata like itna bhi hard nhi sochna tha😊

  • @aws_handles
    @aws_handles 9 หลายเดือนก่อน +17

    Netflix ❌
    MIK’s Long Detailed Videos ✅

  • @wearevacationuncoverers
    @wearevacationuncoverers 9 หลายเดือนก่อน +13

    Sir weekly contest ka Qn-3 banado please. Aapse acha koi nahi samjhata.

    • @bhuppidhamii
      @bhuppidhamii 9 หลายเดือนก่อน +1

      +1

    • @codestorywithMIK
      @codestorywithMIK  9 หลายเดือนก่อน +2

      Weekly Contest 377 Qn - 3
      th-cam.com/video/M8WnAIhTjmQ/w-d-xo.htmlsi=U8onTOhsee0T7Q7M

  • @sauravchandra10
    @sauravchandra10 9 หลายเดือนก่อน +6

    Was able to solve this on my own! Thanks to your previous videos for teaching me the concept.
    My implementation:
    class Solution {
    public:
    int dp[101];
    int solve(int ind, string &s){
    if(ind==s.size()) return 1;
    if(dp[ind]!=-1) return dp[ind];
    int ans=0;
    int num=s[ind]-'0';
    if(num!=0){
    ans+=solve(ind+1,s);
    }
    if((num==1 || num==2) && ind

  • @wearevacationuncoverers
    @wearevacationuncoverers 9 หลายเดือนก่อน +1

    Just completed the video. Bottom Up ka fear bhaga Diya sir apne. Tussi great ho.

  • @qR7pK9sJ2t
    @qR7pK9sJ2t 7 หลายเดือนก่อน +2

    Yrr you have such a soothing voice..
    Please try in dubbing /RJ roles as well..
    And you can continue to post videos as well..

    • @codestorywithMIK
      @codestorywithMIK  7 หลายเดือนก่อน

      Means a lot ❤️❤️🙏🙏

  • @pragatijadhav26
    @pragatijadhav26 9 หลายเดือนก่อน +3

    its request please provide solution and explanation of weekly and biweekly contest questions also.

    • @codestorywithMIK
      @codestorywithMIK  9 หลายเดือนก่อน +1

      Weekly Contest 377 Qn - 3
      th-cam.com/video/M8WnAIhTjmQ/w-d-xo.htmlsi=U8onTOhsee0T7Q7M

  • @abhayshukla322
    @abhayshukla322 7 หลายเดือนก่อน

    Bhau jis trah se aap samjhate ho mja aa jata hai mai tree diagram toh pohonch gyabtha but yeh conditions banane me problem after this I am feeling confident thank you 🙏

  • @chayandas9191
    @chayandas9191 9 หลายเดือนก่อน +3

    Please check one biweekly contest question,count the number of incremovable subarray,its difficult to understand how to take the approach,plz check it once if possible sir

    • @codestorywithMIK
      @codestorywithMIK  9 หลายเดือนก่อน +2

      Noted. Let me check this week only ✌️

    • @chayandas9191
      @chayandas9191 9 หลายเดือนก่อน

      @@codestorywithMIK means a lot sir❤️

  • @SrishtiKapoor-d1w
    @SrishtiKapoor-d1w 2 หลายเดือนก่อน +1

    Amazing explanation

  • @souravjoshi2293
    @souravjoshi2293 9 หลายเดือนก่อน +1

    You killed bottom up dude. No one can make tough topics easy like you do.

  • @monikavaid5083
    @monikavaid5083 9 หลายเดือนก่อน +2

    Very detailed and easy to understand explanation

    • @codestorywithMIK
      @codestorywithMIK  9 หลายเดือนก่อน

      Glad it was helpful! 🙏🙏😇

  • @aws_handles
    @aws_handles 9 หลายเดือนก่อน

    Just like my fear of Graph is gone because of your Graph playlist,
    My fear of bottom Up DP is slowly going because of you. I came here to see bottom up and you nailed it 🙌🔥

  • @akshanshsharma6025
    @akshanshsharma6025 9 หลายเดือนก่อน +2

    bro i request you to please start making weekly and biweekly contest videos too it will be great helpful and if you start live stream that will be super helpful

    • @codestorywithMIK
      @codestorywithMIK  9 หลายเดือนก่อน

      Weekly Contest 377 Qn - 3
      th-cam.com/video/M8WnAIhTjmQ/w-d-xo.htmlsi=U8onTOhsee0T7Q7M

    • @akshanshsharma6025
      @akshanshsharma6025 9 หลายเดือนก่อน

      @@codestorywithMIK thanks bro hope you covered all the questions of contest

  • @anuppatankar4294
    @anuppatankar4294 9 หลายเดือนก่อน +2

    Great video 👌

  • @yashkalia2311
    @yashkalia2311 5 หลายเดือนก่อน +1

    Great video

  • @sparshyadav9709
    @sparshyadav9709 หลายเดือนก่อน +1

    Understood.

  • @ankitsahu2305
    @ankitsahu2305 9 หลายเดือนก่อน +1

    Nice explanation

  • @nibaronkumar2484
    @nibaronkumar2484 9 หลายเดือนก่อน +1

    Pura Bawal vaiya 🔥🔥

  • @omgupta7712
    @omgupta7712 9 หลายเดือนก่อน +1

    Sir solved by own top tup approach thanks sir

  • @kingreja5894
    @kingreja5894 9 หลายเดือนก่อน +1

    Just by looking at the logic of recursion from the beginning, I understood your approach. Thankyou so much for clarification bhaiya,
    here is my code :
    public int numDecodings(String s) {
    int n = s.length();
    int[] dp = new int[n+1];
    dp[n] =1;
    for(int i = n-1;i>=0;i--){
    if(s.charAt(i)=='0'){
    dp[i] = 0;
    }else{
    int oneDigit = dp[i+1];
    int twoDigit =0;
    if( i+1 < n && ((s.charAt(i)=='1') || (s.charAt(i)=='2' && s.charAt(i+1)

  • @adarshrathore9960
    @adarshrathore9960 9 หลายเดือนก่อน +1

    Sir please contests ke 3rd, 4th questions ki bhi videos banaiye 🙏

    • @codestorywithMIK
      @codestorywithMIK  9 หลายเดือนก่อน

      Weekly Contest 377 Qn - 3
      th-cam.com/video/M8WnAIhTjmQ/w-d-xo.htmlsi=U8onTOhsee0T7Q7M

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 9 หลายเดือนก่อน +1

    advance congratulations of 14k+ subscription.

  • @sujalgupta6100
    @sujalgupta6100 8 หลายเดือนก่อน

    Thanks for the explantion of tabulation.
    in approach 2 (tabulation) on Github
    it says time and space as both to be o(n)
    but in this way, memoization is more optimal as it has O(n) time and O(1) space
    Also, I couldn't understand the approach 3(bottom up 2) and approach 4
    //t[i] = ways to decode string of length i
    t[0] = 1;
    why is there no check if t[0] is '0' or '1' ?
    One more thing is that, I always stuggle to identify time complexity in question invloving complex data structure or question which involve recursion or solution. I have learnt time complexity concept multiple times, but they always teach easy snippets to explain, but don't explain how to find time and space in lets say Trie Questions. Any remedy for this would help because I want to learn how you tell the time complexity in one go.
    thanks

    • @divyanshmishra5121
      @divyanshmishra5121 3 หลายเดือนก่อน

      you're ignoring the auxiliary stack space for memoization part. for the second doubt, for this part //t[i] = ways to decode string of length i. hence t[0] is actually string of length 0 means no string actually. for t[1] is a string of length 1 which might be '0' hence we're checking

  • @codeandtalk6
    @codeandtalk6 9 หลายเดือนก่อน +1

    ❤❤❤

  • @JoyAcharyatvl
    @JoyAcharyatvl 9 หลายเดือนก่อน +1

    awesome bro

  • @bhartendupant8859
    @bhartendupant8859 9 หลายเดือนก่อน +1

  • @Shivanai_h
    @Shivanai_h 9 หลายเดือนก่อน +1

    Best explanation on

  • @-OmkarSawant-pe2uq
    @-OmkarSawant-pe2uq 9 หลายเดือนก่อน +1

    Great !!!!

  • @Raj10185
    @Raj10185 9 หลายเดือนก่อน +1

    My simple solution :-
    class Solution {
    public:
    int dp[101];
    int solve(int idx , string s )
    {
    if(idx==s.size())
    {
    return 1;
    }

    if(s[idx]=='0')
    return 0;

    if(dp[idx] != -1)
    return dp[idx];

    //way 1 => take 1 char at time
    int count = 0;

    count = solve(idx+1 ,s);

    //way 2 take substring of size 2 but substr must be < "27"
    if(idx< s.size()-1 && s.substr(idx,2) < "27")
    {
    count += solve(idx+2 ,s);
    }

    return dp[idx] = count;



    }

    int numDecodings(string s) {

    memset(dp,-1,sizeof(dp));
    return solve(0,s);


    }
    };

  • @RiyazParve
    @RiyazParve 8 หลายเดือนก่อน

    I am not able to understand why t[n] = 1.
    If s="0" then t[n]=0 how can we assume it will be 1 ?

  • @AmarjeetKumar-to9ub
    @AmarjeetKumar-to9ub 9 หลายเดือนก่อน +2

    🔥++

  • @theOmKumar
    @theOmKumar 8 หลายเดือนก่อน

    thank you!

  • @ammanchhetri6716
    @ammanchhetri6716 9 หลายเดือนก่อน +1

    i was able to solve dis....till space optimization 😪

  • @umeshbisht1054
    @umeshbisht1054 9 หลายเดือนก่อน +1

    Thanku bhaiya ❤

  • @MakeuPerfect
    @MakeuPerfect 7 หลายเดือนก่อน

    dp[i] -> no. of decode ways till length i
    can we solve using this state ? How???

  • @siddharthmishra8651
    @siddharthmishra8651 9 หลายเดือนก่อน +1

    bhai please java ka bhi explain krna video m...:(

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 9 หลายเดือนก่อน +1

    public int numDecodings (String s){
    if (s.charAt(0)=='0') return 0;
    int n=s.length();
    int[]dp=new int [n+1];
    dp[0]=1;dp[1]=1;
    for(int i=2;i

  • @vamsimadugula8524
    @vamsimadugula8524 9 หลายเดือนก่อน

    Space Complexity : O(1)
    class Solution {
    public:
    int numDecodings(string s) {
    int n = s.length();
    int prev=1,prev2=0;
    for(int ind=n-1;ind>=0;ind--){
    int curr = 0;
    if(s[ind]!='0'){
    curr = prev;
    if(ind+1

  • @skfardeenhossain4739
    @skfardeenhossain4739 9 หลายเดือนก่อน +1

    Follow up decode ways 2

  • @AnupGupta-z9x
    @AnupGupta-z9x 9 หลายเดือนก่อน +1

    aazadi ka 75th mahotsav
    😅

  • @tutuimam3381
    @tutuimam3381 9 หลายเดือนก่อน +1

    Thanks 👍

  • @vikashtiwari1521
    @vikashtiwari1521 9 หลายเดือนก่อน

    Kya ye question sirf dp se hi solve ho sakta hai

    • @souravjoshi2293
      @souravjoshi2293 9 หลายเดือนก่อน +1

      There is one constant space solution.
      BUT, if you see, the approach is actually internally using past results (which is nothing but DP only)
      Reference - MIK's Github Repo (Approach-4)
      class Solution {
      public:
      int numDecodings(string s) {
      int n = s.length();
      if(n == 1)
      return s[0] == '0' ? 0 : 1;
      if(s[0] == '0')
      return 0;
      int last1 = 1, last2 = 1;
      for(int i = 1; i

  • @prudhvirajmacherla9854
    @prudhvirajmacherla9854 9 หลายเดือนก่อน +1

    @codestorywithMIK i want to contribute a qns in leetcode can u help me i have previously contributed a qn to leetcode 9 months back but it is in still pending

    • @codestorywithMIK
      @codestorywithMIK  9 หลายเดือนก่อน

      Hi there,
      Actually i have never contributed on leetcode before. That’s why I am also not sure. But let me check

  • @SrishtiKapoor-d1w
    @SrishtiKapoor-d1w 2 หลายเดือนก่อน +1

    Amazing explanation