|
| 1 | +/* LintCode 192 · Wildcard Matching |
| 2 | +Description |
| 3 | +Given an input string s and a pattern p, implement wildcard pattern matching with support for '?' and '*'. The matching rules are as follows: |
| 4 | +
|
| 5 | +'?' Matches any single character. |
| 6 | +'*' Matches any sequence of characters (including the empty sequence). |
| 7 | +The matching should cover the entire input string (not partial). |
| 8 | +
|
| 9 | +Example 1 |
| 10 | +Input: |
| 11 | +"aa" |
| 12 | +"a" |
| 13 | +Output: false |
| 14 | +
|
| 15 | +Example 2 |
| 16 | +Input: |
| 17 | +"aa" |
| 18 | +"aa" |
| 19 | +Output: true |
| 20 | +
|
| 21 | +Example 3 |
| 22 | +Input: |
| 23 | +"aaa" |
| 24 | +"aa" |
| 25 | +Output: false |
| 26 | +
|
| 27 | +Example 4 |
| 28 | +Input: |
| 29 | +"aa" |
| 30 | +"*" |
| 31 | +Output: true |
| 32 | +Explanation: '*' can replace any string |
| 33 | +
|
| 34 | +Example 5 |
| 35 | +Input: |
| 36 | +"aa" |
| 37 | +"a*" |
| 38 | +Output: true |
| 39 | +
|
| 40 | +Example 6 |
| 41 | +Input: |
| 42 | +"ab" |
| 43 | +"?*" |
| 44 | +Output: true |
| 45 | +Explanation: '?' -> 'a' '*' -> 'b' |
| 46 | +
|
| 47 | +Example 7 |
| 48 | +Input: |
| 49 | +"aab" |
| 50 | +"c*a*b" |
| 51 | +Output: false |
| 52 | +*/ |
| 53 | + |
| 54 | +Problem Solving Step Instructions: |
| 55 | +1, If p.charAt(j-1) == s.charAt(i-1) : dp[i][j] = dp[i-1][j-1]; |
| 56 | +2, If p.charAt(j-1) == '.' : dp[i][j] = dp[i-1][j-1]; |
| 57 | +3, If p.charAt(j-1) == '*': |
| 58 | + here are two sub conditions: |
| 59 | + 1. * counts as singe time or empty case: dp[i][j] = dp[i][j-1] |
| 60 | + 2. * counts as multiple times: dp[i][j] = dp[i-1][j] |
| 61 | + |
| 62 | +public class Solution { |
| 63 | + /** |
| 64 | + * @param s: A string |
| 65 | + * @param p: A string includes "?" and "*" |
| 66 | + * @return: is Match? |
| 67 | + */ |
| 68 | + public boolean isMatch(String s, String p) { |
| 69 | + // write your code here |
| 70 | + /*Dp State: dp[i][j]的含义是s[0-i] 与 p[0-i]是否匹配 |
| 71 | + Dp Initialize: dp[0][0] = true |
| 72 | + Dp function: 1. s.charAt(i - 1) = p.charAt(j - 1) => dp[i][j] = dp[i-1][j-1] |
| 73 | + 2. p.charAt(j - 1) = '?' // '?' Matches any single character. |
| 74 | + ==> dp[i][j] = dp[i-1][j-1] |
| 75 | + 3. p.charAt(j - 1) = '*' // '*' Matches any sequence of characters |
| 76 | + ==> * Matches a single time: dp[i][j] = dp[i][j-1] |
| 77 | + ==> * Matches a multiple times: dp[i][j] = dp[i-1][j] |
| 78 | + Dp Answer: return dp[s.length()][p.length()] |
| 79 | + */ |
| 80 | + |
| 81 | + if(s == null || p == null){ |
| 82 | + return false; |
| 83 | + } |
| 84 | + //Dp State: |
| 85 | + boolean[][] dp = new boolean[s.length() + 1][p.length() + 1]; |
| 86 | + //Dp Initialize: |
| 87 | + dp[0][0] = true; |
| 88 | + |
| 89 | + //Preprocessing functon for this kinds solution: s = "aab" , p ="c*aab" |
| 90 | + for(int k = 1; k <= p.length(); k++){ |
| 91 | + if(p.charAt(k - 1) == '*'){ |
| 92 | + dp[0][k] = dp[0][k - 1]; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + //Dp function: |
| 97 | + for(int i = 1; i <= s.length(); i++){ |
| 98 | + for(int j = 1; j <= p.length(); j++){ |
| 99 | + //1. s.charAt(i) = p.charAt(j) |
| 100 | + if(s.charAt(i - 1) == p.charAt(j - 1)){ |
| 101 | + dp[i][j] = dp[i - 1][j - 1]; |
| 102 | + } |
| 103 | + //2. p.charAt(j) = '?' |
| 104 | + if(p.charAt(j - 1) == '?'){ |
| 105 | + dp[i][j] = dp[i - 1][j - 1]; |
| 106 | + } |
| 107 | + //3. p.charAt(j) = '*' |
| 108 | + if(p.charAt(j - 1) == '*' ){ |
| 109 | + dp[i][j] = dp[i][j-1] || dp[i-1][j]; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + //Dp Answer: |
| 115 | + return dp[s.length()][p.length()]; |
| 116 | + } |
| 117 | +} |
0 commit comments