|
| 1 | +/** |
| 2 | + * Implement regular expression matching with support for '.' and '*'. |
| 3 | + * |
| 4 | + * '.' Matches any single character. |
| 5 | + * '*' Matches zero or more of the preceding element. |
| 6 | + * |
| 7 | + * The matching should cover the entire input string (not partial). |
| 8 | + * |
| 9 | + * The function prototype should be: |
| 10 | + * bool isMatch(const char *s, const char *p) |
| 11 | + * |
| 12 | + * Some examples: |
| 13 | + * isMatch("aa","a") → false |
| 14 | + * isMatch("aa","aa") → true |
| 15 | + * isMatch("aaa","aa") → false |
| 16 | + * isMatch("aa", "a*") → true |
| 17 | + * isMatch("aa", ".*") → true |
| 18 | + * isMatch("ab", ".*") → true |
| 19 | + * isMatch("aab", "c*a*b") → true |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | + |
| 24 | +public class RegularExpressionMatching10 { |
| 25 | + |
| 26 | + /** |
| 27 | + * https://leetcode.com/problems/regular-expression-matching/solution/ |
| 28 | + */ |
| 29 | + public boolean isMatch(String text, String pattern) { |
| 30 | + if (pattern.isEmpty()) return text.isEmpty(); |
| 31 | + boolean first_match = (!text.isEmpty() && |
| 32 | + (pattern.charAt(0) == text.charAt(0) || pattern.charAt(0) == '.')); |
| 33 | + |
| 34 | + if (pattern.length() >= 2 && pattern.charAt(1) == '*'){ |
| 35 | + return (isMatch(text, pattern.substring(2)) || |
| 36 | + (first_match && isMatch(text.substring(1), pattern))); |
| 37 | + } else { |
| 38 | + return first_match && isMatch(text.substring(1), pattern.substring(1)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + public boolean isMatch2(String text, String pattern) { |
| 44 | + return isMatch(0, 0, text, pattern); |
| 45 | + } |
| 46 | + |
| 47 | + public boolean isMatch(int i, int j, String text, String pattern) { |
| 48 | + if (j == pattern.length()) return i == text.length(); |
| 49 | + boolean first_match = (i != text.length() && |
| 50 | + (pattern.charAt(j) == text.charAt(i) || pattern.charAt(j) == '.')); |
| 51 | + |
| 52 | + if (pattern.length() >= j+2 && pattern.charAt(j+1) == '*'){ |
| 53 | + return (isMatch(i, j+2, text, pattern) || |
| 54 | + (first_match && isMatch(i+1, j, text, pattern))); |
| 55 | + } else { |
| 56 | + return first_match && isMatch(i+1, j+1, text, pattern); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + public boolean isMatch3(String text, String pattern) { |
| 62 | + int textLen = text.length(); |
| 63 | + int pattLen = pattern.length(); |
| 64 | + boolean[][] dp = new boolean[textLen+1][pattLen+1]; |
| 65 | + dp[0][0] = true; |
| 66 | + |
| 67 | + for(int j=1; j<=pattLen; j++) { |
| 68 | + if (pattern.charAt(j-1) == '*'){ |
| 69 | + dp[0][j] = dp[0][j-2]; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + for (int i=1; i<=textLen; i++) { |
| 74 | + for(int j=1; j<=pattLen; j++) { |
| 75 | + if (j >= 2 && pattern.charAt(j-1) == '*') { |
| 76 | + boolean preMatch = (pattern.charAt(j-2) == text.charAt(i-1) || pattern.charAt(j-2) == '.'); |
| 77 | + dp[i][j] = dp[i][j-2] || (preMatch && dp[i-1][j]); |
| 78 | + } else { |
| 79 | + boolean currMatch = (pattern.charAt(j-1) == text.charAt(i-1) || pattern.charAt(j-1) == '.'); |
| 80 | + dp[i][j] = currMatch && dp[i-1][j-1]; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return dp[text.length()][pattern.length()]; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + /** |
| 90 | + * https://leetcode.com/problems/regular-expression-matching/solution/ |
| 91 | + */ |
| 92 | + enum Result { |
| 93 | + TRUE, FALSE |
| 94 | + } |
| 95 | + Result[][] memo; |
| 96 | + |
| 97 | + public boolean isMatch4(String text, String pattern) { |
| 98 | + memo = new Result[text.length() + 1][pattern.length() + 1]; |
| 99 | + return dp(0, 0, text, pattern); |
| 100 | + } |
| 101 | + |
| 102 | + public boolean dp(int i, int j, String text, String pattern) { |
| 103 | + if (memo[i][j] != null) { |
| 104 | + return memo[i][j] == Result.TRUE; |
| 105 | + } |
| 106 | + boolean ans; |
| 107 | + if (j == pattern.length()){ |
| 108 | + ans = i == text.length(); |
| 109 | + } else{ |
| 110 | + boolean first_match = (i < text.length() && |
| 111 | + (pattern.charAt(j) == text.charAt(i) || |
| 112 | + pattern.charAt(j) == '.')); |
| 113 | + |
| 114 | + if (j + 1 < pattern.length() && pattern.charAt(j+1) == '*'){ |
| 115 | + ans = (dp(i, j+2, text, pattern) || |
| 116 | + first_match && dp(i+1, j, text, pattern)); |
| 117 | + } else { |
| 118 | + ans = first_match && dp(i+1, j+1, text, pattern); |
| 119 | + } |
| 120 | + } |
| 121 | + memo[i][j] = ans ? Result.TRUE : Result.FALSE; |
| 122 | + return ans; |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments