DP 34. Wildcard Matching | Recursive to 1D Array Optimisation 🔥

Поділитися
Вставка
  • Опубліковано 12 вер 2024

КОМЕНТАРІ • 483

  • @mohitagarwal3639
    @mohitagarwal3639 2 роки тому +168

    Hey Striver you have helped me a lot in logic building .
    I paused the video right at 4:42 after understanding what question is all about and was able to code the solution all by myself.
    It feels really great when I solves a question (all by myself) which was next to impossible for me a month back.
    I always thank you the moment green signal of "Correct Answer" pops up and you deserve it.
    Understood🙂🙂

  • @sayandey2492
    @sayandey2492 Рік тому +24

    I remember about 2 months back, I had seen this problem and closed it after reading the problem statement for 2 minutes, understanding it can't be solved by me in any way. Today I am able to solve it within 30 minutes without seeing the video at all 🤯. Thanks Striver. You have love of all aspiring coders 🙌 🙌

  • @melvinfrancis7525
    @melvinfrancis7525 2 роки тому +64

    In tabulation to check the base case instead of iterating from the beginning every time you can simply check if the previous state is true and the current value = '*".
    for(int i = 1 ; i

    • @parthsalat
      @parthsalat 2 роки тому +3

      Dev Manus

    • @cheriviralakarthik
      @cheriviralakarthik 2 роки тому

    • @ankitmeena5637
      @ankitmeena5637 Рік тому

      can you explain it more clearly ?? please

    • @kaushik.aryan04
      @kaushik.aryan04 Рік тому

      you are a real one

    • @anonymous10906
      @anonymous10906 10 місяців тому

      Previous state 1 signifies that upto that index all characters are '*' and if the current index in wild string is also '*' then the wild string upto the current index has all its characters as '*'@@ankitmeena5637

  • @toshangupta7306
    @toshangupta7306 2 місяці тому +4

    More optimized version
    for computing 0th index of curr --> It is checking if the string p contains only *
    If previously we found 0th index to be false that means it contains char other than * --> Assign false
    Otherwise if previously it was true --> If curr index of string is * --> true else ->> false
    bool isMatch(string s, string p) {
    int m=s.length();
    int n=p.length();
    vectorprev(m+1,false),curr(m+1);
    prev[0]=true;
    for(int i=1;i

  • @stolenkid2121
    @stolenkid2121 2 роки тому +33

    I messed up this question with regular expression matching, I think regex matching is a bit tougher than this, thank you for this explanation, I hope the next lecture will be regex matching one . I solved that regular expression matching but would like to hear again from you😊

    • @takeUforward
      @takeUforward  2 роки тому +8

      Its the same question !

    • @amitkoushik5504
      @amitkoushik5504 Рік тому +5

      @@takeUforward it's similar not same

    • @saibingi6980
      @saibingi6980 Рік тому +2

      @@amitkoushik5504 yes

    • @arunm619
      @arunm619 3 місяці тому

      @stolenkid2121 how is it different from Wildcard matching

    • @brokegod5871
      @brokegod5871 2 місяці тому

      How did you solve it?

  • @gauravbhardwaj6656
    @gauravbhardwaj6656 2 роки тому +30

    for more optimization we can merge multiple continuous '*' with single '*' before applying DP and we can reduce those last 1d array to single integer value.

  • @gaurishaaaa
    @gaurishaaaa 2 місяці тому +2

    DP on strings understood ✅
    From hating and fearing DP to love doing it.. all the way grateful to you.

  • @yashshetty7326
    @yashshetty7326 Рік тому +17

    I could have never thought that I could solve a Leetcode hard question by myself. This dp playlist is the best playlist in UA-cam and I thank you Striver for such an amazing playlist.

    • @deviprasad_bal
      @deviprasad_bal Рік тому +1

      same dude, i just can't believe how easy striver made string matching dp for me, I did this myself and I can't believe it.

    • @romanshrestha7510
      @romanshrestha7510 25 днів тому

      same dude i can't believe this

  • @adebisisheriff159
    @adebisisheriff159 8 місяців тому +4

    I do not know how to thank you Striver..... Thank you for being an amazing person and coder.
    Thank you for being a shoulder to lean on..... 🥰🥰🥰🥰🥰🥰🥰

  • @prerna_mishra
    @prerna_mishra 2 роки тому +23

    This series has brought me to the point where I can build intuition and code by myself within minutes ( which was unimaginable approx 15 days back). All thanks to you nd your constant efforts!! Extremely Grateful _/\_

  • @ahanavishwakarma3956
    @ahanavishwakarma3956 2 роки тому +7

    I was able to think of almost the whole approach without watching your explanation. Couldn't be more grateful!

  • @abhishekNITT
    @abhishekNITT Рік тому +1

    Base Case for Tabulation
    dp[0][0]=true;
    if(pattern[0]=='*')
    {
    int i=1;
    while(i

  • @codermal
    @codermal 2 роки тому +1

    We can further optimize by keeping a flag and checking for current characters only in base case
    (Ex: pattern = ****ab, text=ab)
    i.e:
    int m=pattern.size(),n=text.size();
    vector prev(n+1,false),temp(n+1,false);
    prev[0]=true;
    bool flag=true;
    for(int i=1;i

  • @mdmudassiralam5490
    @mdmudassiralam5490 2 роки тому +4

    We can make the base case simpler
    if(ind1

  • @raghuvamsi4879
    @raghuvamsi4879 Рік тому +2

    Tysm striver, the way you explained these dp problems so far is really really awesome, In fact I was able to come up the last 3 problems in "DP on Strings" set by myself! This is all because, your explanation for previous questions is damn good which made me understand the true essence of dp! Keep the 🔥going and all the very best striver ❤!

  • @mantrarajgotecha3055
    @mantrarajgotecha3055 2 роки тому +11

    You just not make us understood dp on strings but you make us feel how to think and feel about dp on strings.
    Hats off you !!

  • @ankitpandey7262
    @ankitpandey7262 2 роки тому +4

    Anyone following the playlist from the start can able to do these Leetcode Hard Ques easily without watching the video.
    Thanks for this amazing content.

  • @parthsalat
    @parthsalat 2 роки тому +10

    *Notes*
    1. Those who already know the question can start the video from 4:40
    2. Using .at() operator for string and vectors does automatic bound checking. It's much safer than using [ ]
    😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏
    *Important timestamps*
    Recursion
    - code: 29:47
    - complexity: 26:00
    Memoization
    - code: 31:00
    - complexity: 26:55
    Tabulation code: 38:21
    Space optimisation code: 42:26
    😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏😏
    *Striver's this code snippet (**38:07**) runs in O(N^2) time. I've optimised it to run in O(N) time*
    // ---------------------------------- Code snippet begins ----------------------------------------
    //if there is no text, the pattern must be all * for answer to be true
    for(int patternIndex=1; patternIndex

    • @Nutrino259
      @Nutrino259 12 годин тому +1

      😏😏😏😏😏😉😏😏😏😏😏

  • @vishious14
    @vishious14 8 місяців тому +2

    Striver, you’re the greatest teacher I’ve ever had. I was able to build the logic for this question and the previous question all by myself and solved in all three approaches. Thanks a lot man. This is GOLD !!!!

  • @devanshkumar3816
    @devanshkumar3816 2 роки тому +3

    The way you taught the space optimization in previous videos it got onto my tip and was able to space optimize the code on my own...
    Much respect !!! orz
    I did space optimisation this way:
    int n = s.length(), m = t.length();
    vector dp(m + 1, false);
    dp[0] = true;
    for(int j = 1; j

  • @ChandraSekhar-mz8xy
    @ChandraSekhar-mz8xy 2 роки тому +6

    I would also like to add :
    > curr[0] depend on prev[0] and if the current char is '*' o r not.
    > instead of 'isAllStars' method (given in article) you can use this single line :
    curr[0] = p.charAt(i-1) == '*' && prev[0];

  • @naman_goyal
    @naman_goyal 2 роки тому +6

    Amazing content. Now I start solving before watching video and then watch it completely to learn how to explain better and writing clear code. Thankyou ❤️

  • @vaibhavagarwal1479
    @vaibhavagarwal1479 8 місяців тому

    we can also use this two line code for setting curr[0] and it saves some time as well just we also have to initialize flag as true outside the first for (i) loop and this will be placed in inside first for loop just like striver's.. Thankyou striver for all the efforts
    if(flag == true && pattern[i-1] != '*')flag = false;
    curr[0]=flag;

  • @ajitpalsingh606
    @ajitpalsingh606 Місяць тому

    Here Striver is assuming S1 to b ehaving character * and ? and S2 string that is to be matched.
    But In Leetcode ,there is opposite S1 string is to be matched and S2 string contains * and ?
    therefore ,Base condition will change
    if(i

  • @kunalgupta343
    @kunalgupta343 Місяць тому

    I think we don't need nested loop for *,
    for(let j=1; j

  • @kazuma0803
    @kazuma0803 2 роки тому +23

    First hard question done on my own. Thank you Striver

    • @amitkoushik5504
      @amitkoushik5504 Рік тому +2

      I also solved it till space optimization on my own ....and started dancing😂❤❤🎉all credits to striver🎉❤

    • @GodOfFaith
      @GodOfFaith Рік тому +4

      you worte the code on your own , doing question on your own means , you found the logic on your own , which you didn't but still its good that you solved the problem

  • @googleit2490
    @googleit2490 10 місяців тому +1

    Done and dusted in the DP revision :)
    (31 mins)
    Nov'17, 2023 10:11 pm

  • @champion5946
    @champion5946 Рік тому

    US
    for base case In C++ we can also do
    for(int i = 1; i

  • @Your_Boy_Suraj
    @Your_Boy_Suraj Рік тому +2

    Wrong Answer
    276 / 354 testcases passed
    Input
    s =
    "aab"
    p =
    "c*a*b"
    Use Testcase
    Output
    false
    Expected
    true

    • @KaifKhan-sb2yr
      @KaifKhan-sb2yr 9 місяців тому

      same with me did you got the solution ?

  • @tn4122
    @tn4122 2 роки тому +4

    Is the code working for everyone in leetcode? If yes please share the code. I'm not getting all the test cases passed.

    • @maxtennyson01
      @maxtennyson01 Рік тому +1

      just sawp the strings at the beginning and try again

  • @pranavm002
    @pranavm002 Рік тому +3

    On Leetcode : only 1731/1811 passes in the bottom up (tabulation) apparoch
    "*a?b*"
    ""zacabz" This testcase fails for some reason

  • @koushalsharma7041
    @koushalsharma7041 2 роки тому +3

    Understood the entire DP on Strings. Thankyou so much for the quality videos !!!!!!!!!!!!

    • @ani68
      @ani68 2 роки тому

      +1

    • @ArnabJhaYT
      @ArnabJhaYT 2 роки тому

      @@ani68 found the legend aniruddha :)

  • @ganeshkamath89
    @ganeshkamath89 2 роки тому +3

    Thanks Striver. Understood.
    Why have you done bitwise or (|) instead of logical or (||) for asterisk case?

    • @anuragpandey8165
      @anuragpandey8165 2 роки тому +1

      It is because the only possible return values of function are 0 and 1. Hence the bitwise OR can return the correct answer as 0 | 0 = 0, 0 | 1 = 1, 1 | 0 = 1, 1 | 1 = 1. Also the bitwise operations are faster and striver is a CP guy.

    • @ganeshkamath89
      @ganeshkamath89 2 роки тому

      @@anuragpandey8165 , why have you written 0 | 0 = 1?
      Also are you sure that bitwise or is faster? because even logical or does the same
      0||0 = 0
      0||1 = 1
      1||0 = 1
      1||1 = 1
      Where as bitwise or does the operation bitwise (agreed that we are dealing with 1 bit here, but still I am not convinced).

  • @amanbhadani8840
    @amanbhadani8840 2 роки тому +9

    I loved the way you handled all the valid number of charcters possible starting from zero for astrik.I thought of looping it but your explanation was so much simple and easy to understand.

  • @sauravchandra10
    @sauravchandra10 Рік тому +3

    Understood the entire DP on strings, thanks a lot!

  • @stith_pragya
    @stith_pragya 8 місяців тому +1

    UNDERSTOOD.....Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @anhadwadhwa2844
    @anhadwadhwa2844 2 роки тому +1

    We can optimise this further using a single 1-d array as well, by taking a variable storing prev value at j-1 index. Moreover, the third base case can be optimised to curr[0]=prev[0]&&(p[i-1]=='*');
    and the code for 1-1d array is:
    vector prev(m+1, 0);
    prev[0]=1;
    for(int i=1; i

  • @dsp-io9cj
    @dsp-io9cj 11 місяців тому

    dp[0][0]=1;
    for(int i=1;i

  • @shubhamsalunkhe137
    @shubhamsalunkhe137 2 роки тому +2

    Understood
    It was great learning DP on strings from your striver🙌🙌
    Thank you for everything you have done🙌

  • @johar3737
    @johar3737 Рік тому +2

    Understood. Now I am able to make logic and intuition behind the question. Thanks Striver

  • @sauravlogsvio1891
    @sauravlogsvio1891 2 роки тому

    This is awesome.
    But the base case -> where pat[i] remains.. while string is exhausted, probably needs some more treatment.
    For example - "c*a*b" matches with "aab" because - c* means 0 c's or any number of characters. For instance, ab*c matches "ac", "abc", "abbc", "abbbc", and so on..
    Need to check if all the chars are '*' or its (char) followed by '*' like [w*x*y*....] kind of pattern which means there can be no characters at all.

    • @chetanraghavv
      @chetanraghavv 2 роки тому

      I think you are confusing this question with Regular Expression Matching
      Here, "c*a*b" does not match "aab"
      because, if first * is a and second is NULL then string becomes "caab" which does not match "aab"

  • @prabhakaran5542
    @prabhakaran5542 5 місяців тому +1

    Understood ❤

  • @user-is6ky7pp2n
    @user-is6ky7pp2n Місяць тому

    Sir Hatts Off to you, Keep Going...

  • @Parthj426
    @Parthj426 2 місяці тому

    #Understood | Wonderful series of DP on strings | Definitely feeling confident !

  • @ameypate9610
    @ameypate9610 Місяць тому

    Approach to this problem is explained well, and optimizations are interesting to perform.

  • @arijitdas4560
    @arijitdas4560 2 роки тому +1

    in 24:34 there is nother case that if in s1[ind]=='?' then also I can return true....but here the else case is hadling that part...done wisely

  • @hashcodez757
    @hashcodez757 Місяць тому

    "UNDERSTOOD BHAIYA!!"

  • @ibtesamansari
    @ibtesamansari 7 місяців тому

    Bro , I literally enjoyed this lecture, I am filled with joy , Awesome explanation

  • @mohaimenchowdhury
    @mohaimenchowdhury 7 місяців тому

    Hey Striver!! I am a great fan of your videos. Can you make series for the other string matching algorithms like KMP/Rabin Karp also? I know you are the best person right now to teach those at very intuitive level.

  • @lohesh9822
    @lohesh9822 2 місяці тому +1

    For the base case, couldnt you just write ,
    for(int i=1;i

  • @ajitpalsingh606
    @ajitpalsingh606 Місяць тому +1

    Done Recursion on my own

  • @OMPRAKASH-is7fj
    @OMPRAKASH-is7fj 10 місяців тому +1

    greatful to u

  • @UECAshutoshKumar
    @UECAshutoshKumar 2 місяці тому +1

    Understood !

  • @abhinavgupta4778
    @abhinavgupta4778 Рік тому

    Just an observation: Space optimization into two array will be very easy if you make the dp matrix as dp[text.size][pattern.size] rather than dp[pattern][text].

  • @hitengarg3167
    @hitengarg3167 2 роки тому +1

    Keep up the good work Bro!!🔥🔥🔥. Always scared of this question, whenever I see it, I immediately skip it...But you made it so so so simple to understand, that without even looking at the solution I was able to come up with code. The entire DP series is AWESOME🔥🔥🔥

  • @deependrasinghshekhawat8107

    Solved this questions along with Regular Exapression matching, all on my own, All thanks to you Striver!!!

  • @akshayjain8810
    @akshayjain8810 2 роки тому +2

    can we run a loop when pattern[ i ] == ' * ' and return accordingly

  • @jasmeenkaur6001
    @jasmeenkaur6001 2 роки тому +1

    thank you so much striver...................... First time in my life i understood tabulation and able to write code by myself. thankyou so soooooooooooo muchhhhhhhhhhhhhhhhhhhhhh.

  • @sahilgagan2242
    @sahilgagan2242 2 роки тому +3

    61% done bro thankyou so much ....

  • @sarveshsugandhbarnwal9809
    @sarveshsugandhbarnwal9809 2 роки тому +4

    So blessed to have teacher like You, Very clean explanation. Thank You Striver

  • @arpankoley4256
    @arpankoley4256 8 місяців тому

    For the leetcode version pattern p is declared second , so in 1D array optimization i have taken prev array as dp[m+1] = {false};
    Here is a simplified base case for this-->
    dp[0] = true;
    for(int j=1;j

  • @ztrixx3280
    @ztrixx3280 2 роки тому +1

    when i was trying to solve this question, i thought of looping through until i find next character to star(*), recursion doesn't even came across my mind at 5:51 (video timestamp)
    after getting WAs i realised that it was meant to be solve the way Raj explained.

  • @sarthakyadav9950
    @sarthakyadav9950 Рік тому

    Instead of using two 1-D Arrays we can further space optimize it using a single array. For every index we require value of it's prev row and prev col if char matches and if it does not match we require prev row same col and same row prev col. We just have to maintain a variable that can store prev row prev col value and have to update it accordingly.

    • @karankaira
      @karankaira 7 місяців тому

      @sarthakyadav9950 can u pls paste the code here , i tried but it is failing for "a" and ""

  • @fanofabdevillersandmathslo5960
    @fanofabdevillersandmathslo5960 16 днів тому +1

    Understood

  • @AyushGupta-re5yp
    @AyushGupta-re5yp Рік тому +1

    This is legit insane! Gold Content.
    1st Hard Problem on my own. All credits to you!

  • @joshrak3412
    @joshrak3412 Рік тому

    base case in tabulation for this condition (i>=0 && j

  • @pratyakshsinha6292
    @pratyakshsinha6292 Рік тому +2

    fails for test case
    pattern = "*"
    text = "aa"

  • @felixkimbu8484
    @felixkimbu8484 Рік тому

    Was very helpful. My problem did not require '?' so I just removed it from the condition. Also, loops where not allowed so I created another recursive function to use it in place of the for loop.

  • @suhaanbhandary4009
    @suhaanbhandary4009 Рік тому +1

    Understood!!, and solved this question before explanation, Thanks for such series !!

  • @suyashjain3223
    @suyashjain3223 10 місяців тому

    Completed DP on strings successfully!!

  • @preetisahani5054
    @preetisahani5054 11 місяців тому

    Awesome explanation, I missed the all stars base case while solving on my own,

  • @ritikshandilya7075
    @ritikshandilya7075 2 місяці тому

    Thankyou so much for great explanation Striver

  • @pranavsharma7479
    @pranavsharma7479 2 роки тому +2

    this ques and regex ques are completely different, dont confuse b/w them

  • @keshavprasad1017
    @keshavprasad1017 10 місяців тому

    You made DP feel like a cup of coffee ☕️ ...

  • @AsifHussain-gl4ci
    @AsifHussain-gl4ci 3 місяці тому +1

    what will happen if * or ? are in s2 i am not understanding how that case is considered

  • @rahultiwari7714
    @rahultiwari7714 2 роки тому +11

    hats down sir!!!
    this is happen when god himself came on earth to teaches us this beautiful concept...
    and thank u soo much for whatever u r doing for us...
    and mark my word agar coding and logic building me revolution aayi toh voh appke vajah se hi aayegi... thank u soooo os ssoooo much

  • @electricindro2236
    @electricindro2236 7 місяців тому

    Amazing explanation! Understood!

  • @mayankbhugra6193
    @mayankbhugra6193 2 роки тому +2

    Amazing !!! explanation and energy. Thanks for the content ❤

  • @original_gangsta_
    @original_gangsta_ 2 роки тому +1

    What if the second string is : "abcbdef"? Will this solution work for this test case also?🤔

  • @shisui3645
    @shisui3645 Рік тому +1

    Gives an error for pattern="*" and text="aa"

  • @RohitKumar-dy2gc
    @RohitKumar-dy2gc 10 місяців тому

    just saw the recursion and rest is super super simple

  • @LowkeyCoder
    @LowkeyCoder 9 місяців тому

    "Understood"thank you!

  • @Hrushi_2000
    @Hrushi_2000 11 місяців тому

    Understood. Thankyou Sir.

  • @DevashishJose
    @DevashishJose 8 місяців тому

    Understood thank you so so much.

  • @tasneemayham974
    @tasneemayham974 11 місяців тому

    BESTT TEACHERR EVERR!!!

  • @shre_yash.
    @shre_yash. Рік тому

    use i for text and j for pattern easier space optimization

  • @chanchalroy3417
    @chanchalroy3417 8 місяців тому

    Just wow 😮 understood 👏❤️

  • @aayushranjan3675
    @aayushranjan3675 Рік тому

    For handling the '*' case, can we do it by using a for loop for calculating the length to be matched and then call the recursive function? The runtime becomes very slow on doing this. Can you tell me why this happens as the number of recursive calls would be approx the same for both the cases.
    Thanks.

  • @pk4288
    @pk4288 2 роки тому

    I have a doubt.. should it be this '|' or '||' , for or sign

  • @abhirupray2494
    @abhirupray2494 8 місяців тому

    Understood completely

  • @inforook2258
    @inforook2258 Рік тому

    Inspite of this code to check for the base case s2 is empty and s1 contains '**...' or not .
    for(int i=1;i

  • @deepakmehra9889
    @deepakmehra9889 Рік тому

    can we use for loop in case of *

  • @arpnasjs9825
    @arpnasjs9825 Рік тому

    Did space optimization on my own in a different way...Thank u for this series.

  • @nishantraj7209
    @nishantraj7209 День тому

    if constraint start from 0 then how to handle those cases

  • @dheerajsharma5492
    @dheerajsharma5492 2 роки тому +1

    I got it. hats off for space optimization. 👌

  • @cinime
    @cinime 2 роки тому

    Understood! Is this the basis of how regexp match with '*' or '?' is implemented?

  • @shubhgaming3348
    @shubhgaming3348 2 місяці тому

    understood bhaiya

  • @aniruddhakotal1534
    @aniruddhakotal1534 11 місяців тому

    Till how much optimized is needed in interviews? will they expect space optimized, or , till tabulation is mandatory?

  • @shivamprajapati407
    @shivamprajapati407 2 роки тому

    a doubt...can we not first find the lcs and then decrement the jth pointer by length of str2-lcs in that way we could overcome the recursion for finding the length ' * ' could take....?

  • @gunduboinadileep9523
    @gunduboinadileep9523 9 місяців тому

    understood!!. thank you