Dynamic Planning | String matching with wildcard characters (easy to understand)

Source: Internet
Author: User

string matching with wildcard characters First, Leetcode | Wildcard Matching (only one string contains wildcard characters)

The topic is simple, that is, two strings, one containing wildcards, to match another string, and the output of two strings is consistent.

Note: '? ' means match any character, ' * ' means match any character 0 or more

First, we think of brute force. If the break from beginning to end, to the second character, whether the match succeeds depends on whether the first character matches successfully! So we think that we should use dynamic programming;

Since the dynamic programming is used, the most important thing is to set the initial value and find the recursive type:

So, we start to analyze how to set the initial value, in fact, it is very simple, the matching problem can be imagined as a matrix DP, the vertical axis represents the matching string containing the wildcard character S2, the horizontal axis represents the string S1 to match. Suppose now s2= "a*b", s1= "abc"

The corresponding vacancy is the current (I,J) position and whether the two strings match. Matches T (true), does not match F (false), and finally returns the bottom-most-right value, which is the final value of the current two-string match;

Now we have to set the initial value, so we can add a row and a column, to fill the initial value; S1 since we are going to match, we are all set to F (that is, dp[0][1]=f,dp[0][2]=f,dp[0][3]=f), which means that the match is not currently started. And S2 's initial value, we found that if the asterisk and a swap position, the asterisk can match any string, so the dp[i][0] depends on whether the position is an asterisk and the previous position d[i-1][0] is T (actually the previous position is an asterisk), so we set dp[0][0] to T. So form:

At this point the initial value has been set, we want to find recursion, we find that recursion should have two, one is when the S2 character is an asterisk, the other is the S2 character is non-asterisk.

First look at the asterisk: when you want to calculate dp[2][1] (that is, to match a * and a), we find that it depends on dp[1][1] (that is, a and a match), when you want to calculate dp[2][2] (that is, to match a * and AB), depends on dp[2][1] (that is, a * and a match). Abstract, asterisks and any characters (0 or more) match. So the string is matched to the asterisk, depending on the current position up and to the left (that is, it can be 0 characters or multiple characters). So at this point the recursive type is d P[I][J]=d P[I?1][J]||d P[I][J?1]

See also non-asterisks: when calculating dp[3][2] (i.e. to match a*b and AB), depends on dp[2][1] and a[3][2] (i.e. whether a * and a match, while B and B match); So you can get a recursive dp[i][j] = DP [I-1] [J-1]&&a[i][j].

Finally, we get the initial value and two recursive type, it can be on the code;

//ismatch:s1 No wildcard, S2 have wildcard characters, '? ' means match any character, ' * ' means match any character 0 or more     Public Static Boolean IsMatch(string s1, string s2) {intCountxing =0; for(CharC:s2.tochararray ()) countxing++;if(S2.length ()-countxing > S1.length ())//Description s2 Remove the wildcard character, longer than S1            return false;//Dynamic programming set initial value        Boolean[] DP =New Boolean[S2.length () +1][s1.length () +1]; dp[0][0] =true; for(intI=1; I<=s2.length (); i++) {CharS2_char = S2.charat (i-1); dp[i][0] = dp[i-1][0] && s2_char==' * ';//Set the initial value of each cycle, that is, the initial value of the matched string is false when the asterisk does not appear in the first place             for(intj=1; J<=s1.length (); J + +) {CharS1_char = S1.charat (J-1);if(S2_char = =' * ') Dp[i][j] = dp[i-1][J] | | dp[i][j-1];//Dynamic plan recursion (asterisk) indicates that the asterisk can match 0 (the result of the last outer loop is determined) or multiple (the result that is determined by the loop in just a moment)                ElseDP[I][J] = dp[i-1][j-1] && (s2_char=='? '|| S1_char = = S2_char);//Dynamic plan recursive (non-asterisk) indicates that the DP value depends on the last state and current state}        }returnDp[s2.length ()][s1.length ()]; }
Two, two strings contain a wildcard solution

Through the above, in fact, this problem is very easy to think about.

The first is the setting of the initial value, and the two strings set the initial value by the string containing the wildcard character in the title, depending on whether it is an asterisk and the previous state.

The second is recursive, it does not change, just need to determine whether the two string contains wildcard characters.

Directly on the code:

 Public Static Boolean Ismatchbyboth(string s1, string s2) {//Dynamic programming set initial value            Boolean[] DP =New Boolean[S2.length () +1][s1.length () +1]; dp[0][0] =true; for(intI=1; I<=s2.length (); i++) {CharS2_char = S2.charat (i-1); dp[i][0] = dp[i-1][0] && s2_char==' * ';//Set the initial value of each cycle, that is, the initial value of the matched string is false when the asterisk does not appear in the first place                 for(intj=1; J<=s1.length (); J + +) {CharS1_char = S1.charat (J-1); dp[0][J] = dp[0][j-1] && S1.charat (J-1)==' * ';if(S2_char = =' * '|| S1_char = =' * ') {Dp[i][j] = dp[i-1][J] | | dp[i][j-1];//Dynamic plan recursion (asterisk) indicates that the asterisk can match 0 (the result of the last outer loop is determined) or multiple (the result that is determined by the loop in just a moment)}Else{Dp[i][j] = dp[i-1][j-1] && (s1_char=='? '|| s2_char=='? '||                    S1_char = = S2_char); }                }            }returnDp[s2.length ()][s1.length ()]; }

The above is just their own understanding, I hope we have a lot of communication!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dynamic Planning | String matching with wildcard characters (easy to understand)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.