/ tusharroy25 github.com/mis... github.com/mis... / tusharroy25 Implement wildcard pattern matching with support for '?' and '*'
Пікірлер: 189
@MasterSergius4 жыл бұрын
Thanks to all Indian guys on KZbin, I'm a well-paid software engineer!
@rajeevsingh54534 жыл бұрын
yo wtf have u posted in yur channel man like wtf?????
@rupesh_dharme2 жыл бұрын
To get all test cases you need a different initialization for the pattern index. You can initialize it with the previous value on the pattern index if the current letter is a '*' or else false.
@dominic_lee2 жыл бұрын
thank you so much for this comment
@abhishekrbb91118 жыл бұрын
I must tell u I failed drastically to understand any DP solution by reading books or online written explanation .. your video has helped me improve drastically . now with ur help i am able to visualise dp based problems
@kitty_tax8 жыл бұрын
Hey Tushar, At 10:25 you are comparing "x?y" and "xa which is essentially T[i-1][j-1] but the code on the white-board states that T[i][j] = T[i][j-1] || T[i-1][j]. If pattern[j]=='?' then T[i][j] = T[i][j-1] || T[i-1][j] || T[i-1][j-1];
@rahulagrawal36113 жыл бұрын
yup correct. Also not clear in 12:05 "2nd case(considering * as non null character)" why the last character of string is removed but * from pattern is not removed. Anybody?
@codewithsunny9813 жыл бұрын
He mistakenly removed the '*' character but explained correctly....comparison should be in x?y* and xa.
@dileepmeena87497 жыл бұрын
can we match empty string ( ' ' ) with * ( because * also match 0th string)..??
@miracledoh40205 жыл бұрын
why do you need to replace multiple ** into one *? we can just look at the previous T[i][j-1] to check for the first row, i.e. //in the case of "a**b" the T will be all false for (int j = 1; j < T[0].length; j++) { if (pattern[j - 1] == '*') { T[0][j] = T[0][j - 1]; } }
@dera_ng2 жыл бұрын
Wow... There is a sort of beauty in dynamic programming.
@securelyinsaycure Жыл бұрын
The only video I have watched so many times at different stages of my career(*pure gold*).
@polavenki7 жыл бұрын
10:44 - we shouldn't wipe "*" in the explanation part?
@vasanthykolluri7 жыл бұрын
totally agree...this is most trickiest part of the algo. @Tushar: Pls add that explanation if possible.Thanks in advance.
@dy09536 жыл бұрын
@Vasanthy, T[i][j-1] represents if * is an empty char then you just need to look at previous match result on the left, T[i-1][j] represents if * matches one or multiple chars, then you look at the top, since top already match the current char.
@sudheerkumar41306 жыл бұрын
Correct , thank god you raised this concern
@nathann42915 жыл бұрын
@@dy0953 what does "since top already match the current char" mean?
@ankuragarwal40145 жыл бұрын
@@dy0953 what does "since top already match the current char" mean? please tell about it
@aviadshiber62322 жыл бұрын
thanks for the video. well explained. when I first approached the problem I thought how can I solve this without any non-constant space. regular expressions need only constant number of states (finite automata) , hence the use of non constant space is redundant (using extra matrix for dp). can you perhaps show how can this be done this way?
@kanikaverma64093 жыл бұрын
Why and how we conclude we need to perform DP at 2:02 sec of video ? How we understand when we need to apply DP ? because DP always optimizations . What's Brute force way ? Guess we need to show that in interview before we jump to DP. Rigth ?
@kamalsmusic8 жыл бұрын
The case for the star where you examine T[i-1][j], is that assuming the star matches the character at i and you are seeing if it matches more?
@guangyang51165 жыл бұрын
Yes, it should be explained in this way. Otherwise, it should be T[i-1][j-1] if we follow the explanation from author, which however is wrong.
@sauravagrawal50135 жыл бұрын
Can be further optimised by using boolean T[][]=new boolean[string.length+1][writeIndex+1]; and changing (int j=1;j
@ialpha64316 жыл бұрын
For case 2: Matching the pattern this way might help in understanding -- b a * b a or b a * b a
@neelesh20233 жыл бұрын
your energy level is on another level
@siddharthkalani75353 жыл бұрын
At 1:11 for pattern a*b string abc, why for this case it is false. * means there could be zero characters. Can you please explain this?
@nikhilbhardwaj60555 жыл бұрын
sir if there is * in pattern then we should check left, upside and 'DIAGONALLY' also otherwise some test cases will be failed
@vivek55625 жыл бұрын
I was also thinking about same. if you match * with "a" then this will fail.
@sameerranjan20394 жыл бұрын
@@vivek5562 * and "a" should match and if u follow this algorithm it does match. It doesn't fail.
@rahulagrawal36113 жыл бұрын
Not clear in 12:05 "2nd case(considering * as non null character)" why the last character of string is removed but * from pattern is not removed. Anybody?
@ymnik1008 жыл бұрын
In the second case, when * is treated as a non-zero sequence we take a value from the top ( T[i-1][j] ) that means * has consumed the i-th character in the string, j still with no change and, as long as I understand, we shouldn't erase * character from the pattern ( kzbin.info/www/bejne/aYuni2CEZaqDjJI ) By the way, want to say thanks to you, Tushar Roy for this video and what you are doing, it is really helpful. Hope to see more videos and tutorials from you.
@apexstate31274 жыл бұрын
x.y*z is not Equal to xaylmz. Because of in Expression y is occure n times including NUll but in String after y->lm is Encounter . So my Question is How * match lm ?
@CodingWithPrakash_4 жыл бұрын
kzbin.info/www/bejne/e4C6eYqMmZpnZrc
@ewibowo744 жыл бұрын
I don't understand about his explanation at 5:02 that * match empty string. However, he wrote F for matrix 0 and *.
@kishores51213 жыл бұрын
If the star would have appeared at the beginning of the pattern then he would've marked it as true, but for this case, where string = "" and pattern = "abc*" it shouldn't show true!
@SWE_69 Жыл бұрын
7 years OLD still GOLD
@AbhishekKumar-oj4zm8 жыл бұрын
tushar sir u r making our path easier
@ShaliniNegi244 жыл бұрын
I am unable to understand when there is * mark what to do?
@techbarikcom4 жыл бұрын
Wow! Explanation was super simple and clear. Thank you so much tushar****
@sahilsoni60908 жыл бұрын
Is it necessary to convert multiple stars into one or can we keep them as it is ?
@jdragon81844 жыл бұрын
u can keep stars that are seperated by ? or even characters , also if missing subsequence is not possible we can use the multiple stars to combine various subsequence to form missing subsequences
@chiragkhubdikar9002 жыл бұрын
Example string: "xacyxay" and pattern "x?y" should return True right because the last three characters are a match, but using this approach it returns False. Or should it return False only?
@dominic_lee2 жыл бұрын
? can only be one character. x*y would match this though
@jellydg9888 жыл бұрын
Thanks for the videos, please keep making them. Will recommend to all my friends~
@SAHILOMATIC1808 жыл бұрын
why is their difference in regex and wildcard solution for pattern[i-1] = '*' for 0 occurance of character before '*'?
@venkatesanr895 жыл бұрын
Why do we need to compute all the values in T[][] ? Incrementing the positions in String and expression till the end using recursion gives the best time complexity ?
@nehaagrawal54578 жыл бұрын
Another awesome explanation!I have a question. I didn't get the part when character is * and you take the value from either top or left. If top and left value don't match , we will get true in some cases and false in others since it's an OR. In your example , if you would have taken false instead of true , final answer would have been different. How does it work?
@cantwaittowatch8 жыл бұрын
Great explanation Tushar. A clarification here, which I surmise you did, but while tracing of the 3rd row, that is 'y' and 4th column where there is a '*', you mentioned at the tail end to take the true value from T[3][3]. Is it that, if any value is true in either T[3,3] or T[2,4] - then fill the resultant T[3][3] with T? Thanks
@asiskumarroy44704 жыл бұрын
is this the same as the regular expression matching dp problem?
@Shabnam_Bandyopadhyay8 жыл бұрын
Hi Tushar , for input , patter= "ab*c" and string= "ac". this algorithm is returning false. though the pattern should accept ac .as it starts with a ends with c and zero occurrence of b. can you please clarify .
@404TimeOut8 жыл бұрын
I think you are confusing wildcard matching with regular expression matching. The meaning of * is different for wildcard and regex. In regex it means match 0 or more occurrences of the character in front of it, in your example, regex ab*c would match ac. But in wildcard matching, * means 0 or more characters, so ab*c would match a word that contains a, b, c, and have 0 or more characters between b and c, since ac does not contain b, it won't match. Wildcard matching is not the same as regular expression matching, though they sound very similar.
@vamsi54krishna6 жыл бұрын
false is expected answer as string "ac" doesn't have 'b' char which is required by pattern.
@kkk74108 жыл бұрын
Normally while checking if a pattern matches a given input ,we start from left of both input and pattern and move to right till the end by comparing both the input and the pattern. In DP should we think in a different way as if we are comparing from right to left and is this is the reason when you have P[j]=* then we have T[i-1][j] as we are expecting more characters for * in input while moving in left direction. This is to understand DP intuitively as the * matching case is little confusing how it works conceptually.
@kkk74108 жыл бұрын
+kk k Also ,if this has to work for right to left,the pattern should be symmetrical ?
@kkk74108 жыл бұрын
+Tushar Roy I believe the example that you showed is from right to left itself and hence when p[j]=='*' and for case where there are more * characters you use T[i-1][j] ? Am I right ?
@kkk74108 жыл бұрын
+Tushar Roy Sorry to bother u so much.Thanks for your time. For a string s[0-i] ,I assume 0 to be at left and going from 0 to i meaning moving right and hence i to be at the right. To decide if T[i][j] is a match which means s[0-i] matches p[0-j] you are first comparing the last characters(right most for the given string which ends at i and pattern which ends at j and the based on ith and jth comparision decrementing either i or j or both by one such as T[i-1][j-1] ) 1)if s[i]==p[j] then T[i][j]=p[i-1][j-1] 2)if p[j]=='*' then T[i][j] = dp[i-1][j] for more * sequence || dp[i][j-1] for zero * sequence. If you were comparing the string and pattern from left to right then for a given string s[0-i] and pattern[0-j] you should start comparing from left i.e zeroth positions of both string and pattern and then move towards end i.e right side.Isnt it ?Also if it is left to right ,for a given T[i][j] for the case when p[j]=='*' and when * is a sequence of characters ,you are taking T[i-1][j] ,shouldn't it be T[i+1][j] ? I understand that this DP is a bottom up approach but for a specific T[i][j] you are starting the comparision from extreme right of both string and pattern and moving towards left. I was tring to solve articles.leetcode.com/regular-expression-matching/ through DP and couldnt understand how to use DP to match * which means 0 or more of previous character and came across your explanation.
@sukanyapai89844 жыл бұрын
Can you also explain the intuition behind why the formula for * and ? cases have come??
@anvesh6874 жыл бұрын
02:05 - exact reason behind the code
@sukanyapai89844 жыл бұрын
@@anvesh687 thanks
@anishsuman13713 жыл бұрын
Why null string is not matched withs star in row 1
@piyushsharma16384 жыл бұрын
Very easy way of explanation , thanks.
@summermoonspace7 жыл бұрын
I finally get to understand wildcard matching (and hopefully regular expression soon)!!! This is the lc question that I've been confused for so long time! I'm crying!!! Thank you so much:)
@queueoverflow2 жыл бұрын
Thanks a lot. I understand when i see the picture.
@ivandrofly6 ай бұрын
This video is crucial for dynamic programing tabulation approach - Thank you Tushar
@winsterjose25744 жыл бұрын
string -> abc & pattern -> a .. as per the logic, won't it return false ?
@winsterjose25744 жыл бұрын
my mistake. It was clearly said in the beginning that, it is not a partial match rather complete one
@maneshwarsingh86884 жыл бұрын
shouldn't T[1][2] be TRUE since we can replace the '?' with an empty string ""?
@srikantadatta86958 жыл бұрын
As usual excellent work Tushar! You rock man! Keep continuing the good work buddy!
@ssjohnsmith33 жыл бұрын
If the "*" is at the start of the pattern taking it from left or above should fail.
@silent_watchmen8 жыл бұрын
Thank you very much Tushar, for the excellent video. However, I had a problem with your algorithm. If I implement with 2D array, it can't pass leetcode OJ (says memory limit exceeded). If I implement with 1D array, it says time limit exceeded. I used C++ and didn't see any extra code compared with your java code. Any thought?
@silent_watchmen8 жыл бұрын
+Tushar Roy Thanks for reply and appreciate if you could take a look. Thanks. tinyurl.com/hrh8qj3
@crystal101-77 Жыл бұрын
Brilliant explaination sir❤
@soumavomondal27056 жыл бұрын
this is some great explanation. could you please introduce the case where there is a special character (say '+') where it matches at least one character of the previous sequence? thanks in advance :)
what is difference between regular exp match and wildcard match?? pls explain me.
@anandtiwari15414 жыл бұрын
in wildcard a* character - any 0 or more alphabet character. - a, ab, aa, ac, aaa in regular a* character - 0 more a character eg- empty, a, aa, aaa, aaaa this one can't be ab
@benjaminrogge49598 жыл бұрын
Thanks for the great video! Is it correct that this algorithm won't work, if you have wildcards in the front of your pattern?
@benjaminrogge49598 жыл бұрын
+Tushar Roy you are right. My initial implementation was wrong
@kitther5 жыл бұрын
Hi, thanks for your video. It's the same excellent. Can I ask one question? Why do we need T[0][0], since there is no [0] letter in neither string nor pattern? Thanks.
@wensun5105 жыл бұрын
It will deal with situation when * represent empty string in the beginning. i.e s:abcd p:*abcd
@kitther5 жыл бұрын
@@wensun510 I suppose the T[1] will include '*' if pattern has it in the beginning?
@wensun5105 жыл бұрын
@@kitther It would be harder to initialize T. i.e String:adc pattern: *a*c, When it comes to T[0][1], that's when you considering if a and *a is a match, you would want to refer to T[0-1][1] or T[0][1-1] according to the algorithm provided. Obviously referring to T[-1][1] would result in intdexOutOfBoundary exception. So you need to consider T[0][1] alone and initialize it and it would be harder. The initialization with empty char in the beginning would be a lot easier, you just have to make '*' match empty char that'll work.
@gulshanjangid34705 жыл бұрын
T[0][0] indicates when both the string and the pattern are empty. Which should return True
@anilduttyemmanuru63593 жыл бұрын
Great explanation. Thank you.
@ryanben3988 Жыл бұрын
I solved it using recursion and memoization. It is because of this video I have realized my Dynamic Programming is trash. Nice video (I think), I did not actually understand it🥲...I will comeback after solving some medium problems with dynamic programming, hopefully I will get the logic behind.
@prat5345 жыл бұрын
Can we extend this logic to contain a delimiter character say '/' whose first instance does not map to '*' of any character ?
@tomma12686 жыл бұрын
Awesome, That's great video. I prefer to keep T[i][j] = T[i][j-1] || T[i-1][j] || T[i-1][j-1] if (pattern[j] == *) without optimization for easy understanding.
@vipulmishra86823 жыл бұрын
Clear and concise explanation!
@anonymoussloth66873 жыл бұрын
at 15:20 you also have to see the case if the pattern in any number of stars in the begininng. For ex: if pattern is "***a" then the T[0][1], T[0][2], T[0][3] all will be true
@TL-fe9si7 жыл бұрын
Your videos help me a lot! I just want to say thank you!
@s1337m7 жыл бұрын
What if a partial match was ok? how would the DP formula change?
@sharma-raju4 жыл бұрын
Thank you so much for a such great explanation
@harishgovindan4 жыл бұрын
Thanks for the video. The solution seems to work fine. However, I would really appreciate if you could comment/explain on how you arrived at this solution. (The usual way to solve this would be to do a character-compare across two strings and use an additional loop for '*' character. That may / may not be the best solution. This would actually avoid a 2-D array and O(mn) complexity... instead it would be O(m) or O(n) right?. But not sure how/why I would arrive at the solution in the video)
@anshusharma118 жыл бұрын
All your videos are awesome.. You are the best teacher.
@vanihemmige5 жыл бұрын
Thanks for the great video and the detailed explanation. I have a doubt at point 5:06 of the video. You said pattern * can match with empty string. Then why is T[0][4] False? Should it not be True?
@DheerajKumarBarnwal5 жыл бұрын
First try to understand what T[i][j] represent, then you will get your answer
@pranayreddy63373 жыл бұрын
The * pattern can be used to match with 0 or more characters..so in your case for NULL string, if pattern itself starts with * then it it okay..BUT as there is already a pattern BEFORE *, NULL can't match with * as it would theoretically mean that the pattern before * has matched to NULL. Got it?
@ankitgomkale115 жыл бұрын
I think the condition for "*" is not correct. At 8:07, T[2][4] should be True as "xa" matches with "x?y*".
@addiegupta5 жыл бұрын
how does xa match with x?y* ?
@ankitgomkale115 жыл бұрын
@@addiegupta '?' can be replaced with any character. Let's say we replace it with 'a'. And '*' means zero or more repetition of previous character. Let's take zero repetition of 'y'. That leaves is with 'xa'.
@himanshijain75858 жыл бұрын
All your videos are really helpful.But i did not get the comparison of '*". How '*' replaces that character and if it does then we will compare pattern: x ? y y with text: x a
@amanpreetsinghbawa16008 жыл бұрын
either * can be used as 0 sequence if that is the case then we get the value from the above cell else if * is used as a sequence > 0 then it means * has been consumed by the current matching so we get the value from the left cell. And the final value is the OR of both the values
@nlharish8 жыл бұрын
Shouldn't the condition in the code be "str[i] == pattern[j]" ?
@nlharish8 жыл бұрын
Nevermind. figured it out.
@abhishes6 жыл бұрын
Doesn't '?' mean that a is optional rather than "any 1 character between a and b"
@xiaofeiguo20918 жыл бұрын
why do you need to compress the multiple "*" into a single "*"?
@rajeshbajya65207 жыл бұрын
Every * represent 0 or more characters, So if you put ***** or * it will be same.
@bantyK5 жыл бұрын
How did you derived the formula at 2:04 ?
@atouba-02 жыл бұрын
Excellent explanation
@curiosull6 жыл бұрын
I know that it is simpler to implement (this method), but it is way harder to understand than a Finite State Machine solution.
@amanpreetsinghbawa16008 жыл бұрын
Tushar excellent recursive relation please include recursive relation in every DP soln. as it gives insight to the problem very quickly. Thanks!!
@anshusharma118 жыл бұрын
if (writeIndex > 0 && pattern[0] == '*') temp[0][1] = true; } +Tushar Roy please clerify : why we are not taking pattern[1]== '*' instead of looking for index 0. As index 0 is always a empty.
@anshusharma118 жыл бұрын
Means index 0 always represent a empty character. So patter[0] contains null. So why we are checking patter[0]=='*'. Shouldn't we instead check pattern[1] == '*' please check at 5.08
@30harshal7 жыл бұрын
Hello, Anshu, I have the same doubt, did you find any explanation or solution further?. Reply me at Harshal Deolekar
@kamalsmusic8 жыл бұрын
Also how do you do it with 1D array?
@Arunk0547 жыл бұрын
Yes it is possible with just O(pattern) space complexity. See my solution here: gist.github.com/arunk054/f78091bf5e44ce5386a8035408a08377
@Arunk0547 жыл бұрын
Btw.. Nice meeting you again @Kamal. Remember Microsoft 2016?
@vsg3216 жыл бұрын
You don't have to remember whole NxM matrix. You just need to remember the previous value on the left and one row of values above the current row.
@kshitijjain85448 жыл бұрын
What an awesome explanation!
@Null_pointer_exceptn8 жыл бұрын
shouldnt "b" be true for a*b regex? i think it should be true as we can have 0 or more occurrence of a.
@Null_pointer_exceptn8 жыл бұрын
Alright.Thanks.
@kitty_tax8 жыл бұрын
+Tushar Roy Thanks for the great video! For regex, the algorithm will remain same apart from some conditions (example - for regex a* - while matching the patter I need to go back 2 columns and check if condition True is possible or not), right?
@DhruvPatel-kg5ut5 жыл бұрын
Why t[0][4] is false?
@shayanmalinda74095 жыл бұрын
It should be true no?
@vitaliistoian5 жыл бұрын
You might be confused because * matches an empty string, however our pattern is not just *. T[0][4] is a match of empty string against the pattern x?y*, therefore it's not a match, so it's false.
@siddharthagarwal86628 жыл бұрын
Good Explanation Tushar!!
@nagarjuna1198 жыл бұрын
why do you remove the chars to match ? @ 6:09 ?
@itsmewaqar7 жыл бұрын
because one * would mean 0 or more characters and 3 * or 10 * or a million * one after other would also mean the same ..
@jinliu59288 жыл бұрын
Great explanation! Thanks for the help!
@cskcsk63706 жыл бұрын
The example mentioned initially is incorrect. For a pattern, a*b, a string "b" should be a match as we are looking for a string with 0 or more "a"s followed by a b. Otherwise a good video.
@mohitgautam63654 жыл бұрын
Thank you it would help a lot to me
@shobhitchaturvediindia8 жыл бұрын
like your videos simple and effective..
@xw76284 жыл бұрын
Roy teaching pattern matching while wearing v clashing patterns
@shariqueshahab4 жыл бұрын
You mean teaching patterns matching while wearing anti pattern dress ?
@user-re6yu6xk6d5 жыл бұрын
Wow, excellent explanation!
@shubhrojyotikabiraj83223 жыл бұрын
For pattern: ****a****b**c and str: dsfsabc your logic is not working. It should give true but all the cells(except first cell) are false therefore ans is ultimately false.
@timbabb2508 Жыл бұрын
I'm here because CoPilot suggested this video in an auto-completed comment. (yes really)
@metaalphabrawlstars76978 жыл бұрын
GOOD EXPLANATION AND THE BEST VIDEO ON KZbin ABOUT THE REGULAR EXPRESSION MATCHING. tHANKS VERY MUCH
@nathann42915 жыл бұрын
t should be T
@Official-tk3nc4 жыл бұрын
He looks like Gautam Gambir. Agree?
@daniyaryeralin98136 жыл бұрын
I love you man! :D I wish I could get you a beer
@Ayoub-adventures3 жыл бұрын
He doesn't drink
@dush5 жыл бұрын
You are god send Tushar
@ivandrofly6 ай бұрын
11:27 - Very important part on the video
@VanjeAv Жыл бұрын
Amazing thank you
@mariochavez35463 жыл бұрын
gracias por este video amigo :)
@maharajak65783 жыл бұрын
no one talks about how the expression comes out first who needs people filling tables, show us how to get the idea for the expression