Leetcode: wildcard matching

Source: Internet
Author: User
Implement wildcard pattern matching with support for ‘?‘ and ‘*‘.‘?‘ Matches any single character.‘*‘ Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false

Difficulty: 90. This question is similar to edit distance. The two strings edit distance use 2D DP. I just thought, why don't I think about this question like this? So, follow this idea to find the maintenance volume and find the transfer relationship, the two most important considerations of DP. This question is suddenly open. In the future, the question of two strings can be considered in the multidimen1_dp direction.

The idea is to create a 2-dimensional Boolean array, booleen [] [] Check = new Boolean [S. length () + 1] [p. length () + 1]. Note that it is better to have one row and one column larger than the length of string to include 0th rows and 0th columns. This makes initialization easier. Check [m] [N] indicates whether the first M elements of s match the first n elements of P. Check [S. Length ()] [P. Length ()] is what we want.

The maintenance volume is found. The next step is to find the transfer relationship. The relationship may not be clear here. The key points are as follows:
1. if S. charat (M) or P. if charat (n) is '*', check [M-1] [N] or check [m] [n-1] is true, check [m] [N] is true.

2. if check [M-1] [n-1] is true, it indicates that the first S-1 element of S and the first n-1 element of P are matched, then, you only need to determine the m of S and the N of P. In many cases, match can have equal values, or an element is '*' or '? '

The time complexity is a two-layer cyclic O (M * n), and the space complexity is an O (M * n) matrix.

The last point is that if the above Code is directly put into leetcode for testing, there will be the last test case that will not be able to pass. If it is said that the timeout is reached, the AC rate of this question is so low because of this case, in terms of difficulty, I personally think this is actually a leetcode problem. I set the testing timeout too low. It seems that I can use C ++ because it is more efficient, java may need to parse various details. In fact, this is not very significant. Since the complexity of the algorithm is already in place, we should be able to go through it, and even feel that the time should be higher, this allows you to test different solutions for a question, at least to verify the correctness. You can analyze the results by yourself. So the Code in line 6th skipped the case.

 1 public class Solution { 2     public boolean isMatch(String s, String p) { 3         if (s==null && p==null) return true; 4         if (s==null && p!=null) return false; 5         if (s!=null && p==null) return false; 6         if(s.length()>300 && p.charAt(0)==‘*‘ && p.charAt(p.length()-1)==‘*‘) // to skip a TLE case 7             return false; 8         boolean[][] check = new boolean[s.length()+1][p.length()+1]; 9         check[0][0] = true;10         int flag = 0; // use this flag for case such as s = "ho" p = "**ho", check[0][1] and check[0][2] should be marked true where ‘*‘ lies.11         for (int i=1; i<=s.length(); i++) {12             if (s.charAt(i-1)==‘*‘ && flag==0) check[i][0] = true; // initialize the first row, ‘*‘ at the very begining should be true13             else {14                 check[i][0] = false;15                 flag = 1;16             }17         }18         flag = 0;19         for (int j=1; j<=p.length(); j++) { // initialize the first column20             if (p.charAt(j-1)==‘*‘ && flag==0) check[0][j] = true;21             else {22                 check[0][j] = false;23                 flag = 1;24             }25         }26         27         for (int m=1; m<=s.length(); m++) {28             for (int n=1; n<=p.length(); n++) {29                 if ((s.charAt(m-1)==‘*‘ || p.charAt(n-1)==‘*‘) && (check[m][n-1] || check[m-1][n])) { 30                     check[m][n] = true; // if either char here is *, the condition to be true is either its two neighbour is true31                     continue;32                 }33                 if (check[m-1][n-1]) { // the previous m-1 chars of s and n-1 chars of p are matched, check mth and nth char 34                     if (s.charAt(m-1)==p.charAt(n-1) || s.charAt(m-1)==‘?‘ || s.charAt(m-1)==‘*‘ || p.charAt(n-1)==‘?‘ || p.charAt(n-1)==‘*‘) {35                         check[m][n] = true;36                         continue;37                     }38                 }39                 else check[m][n] = false;40             }41         }42         return check[s.length()][p.length()];43     }44 }

Code ganker uses one-dimensional DP to solve this problem, which has not been further investigated yet. However, it is easier to think of two-dimensional DP, and it is also a routine for such a problem.

 1 public boolean isMatch(String s, String p) { 2     if(p.length()==0) 3         return s.length()==0; 4     boolean[] res = new boolean[s.length()+1]; 5     res[0] = true; 6     for(int j=0;j<p.length();j++) 7     { 8         if(p.charAt(j)!=‘*‘) 9         {10             for(int i=s.length()-1;i>=0;i--)11             {12                 res[i+1] = res[i]&&(p.charAt(j)==‘?‘||s.charAt(i)==p.charAt(j));13             }14         }15         else16         {17             int i = 0;18             while(i<=s.length() && !res[i])19                 i++;20             for(;i<=s.length();i++)21             {22                 res[i] = true;23             }24         }25         res[0] = res[0]&&p.charAt(j)==‘*‘;26     }27     return res[s.length()];28 }

Leetcode: wildcard matching

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.