|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 44. Wildcard Matching |
5 |
| - * Implement wildcard pattern matching with support for '?' and '*'. |
6 |
| -
|
7 |
| - '?' Matches any single character. |
8 |
| - '*' Matches any sequence of characters (including the empty sequence). |
9 |
| -
|
10 |
| - The matching should cover the entire input string (not partial). |
11 |
| -
|
12 |
| - The function prototype should be: |
13 |
| - bool isMatch(const char *s, const char *p) |
14 |
| -
|
15 |
| - Some examples: |
16 |
| - isMatch("aa","a") → false |
17 |
| - isMatch("aa","aa") → true |
18 |
| - isMatch("aaa","aa") → false |
19 |
| - isMatch("aa", "*") → true |
20 |
| - isMatch("aa", "a*") → true |
21 |
| - isMatch("ab", "?*") → true |
22 |
| - isMatch("aab", "c*a*b") → false |
23 |
| - */ |
24 | 3 | public class _44 {
|
25 | 4 |
|
26 |
| - public static class Solution1 { |
27 |
| - public boolean isMatch(String s, String p) { |
28 |
| - boolean[][] match = new boolean[s.length() + 1][p.length() + 1]; |
29 |
| - match[s.length()][p.length()] = true; |
30 |
| - for (int i = p.length() - 1; i >= 0; i--) { |
31 |
| - if (p.charAt(i) != '*') { |
32 |
| - break; |
33 |
| - } else { |
34 |
| - match[s.length()][i] = true; |
35 |
| - } |
36 |
| - } |
| 5 | + public static class Solution1 { |
| 6 | + public boolean isMatch(String s, String p) { |
| 7 | + boolean[][] match = new boolean[s.length() + 1][p.length() + 1]; |
| 8 | + match[s.length()][p.length()] = true; |
| 9 | + for (int i = p.length() - 1; i >= 0; i--) { |
| 10 | + if (p.charAt(i) != '*') { |
| 11 | + break; |
| 12 | + } else { |
| 13 | + match[s.length()][i] = true; |
| 14 | + } |
| 15 | + } |
37 | 16 |
|
38 |
| - for (int i = s.length() - 1; i >= 0; i--) { |
39 |
| - for (int j = p.length() - 1; j >= 0; j--) { |
40 |
| - if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?') { |
41 |
| - match[i][j] = match[i + 1][j + 1]; |
42 |
| - } else if (p.charAt(j) == '*') { |
43 |
| - match[i][j] = match[i + 1][j] || match[i][j + 1]; |
44 |
| - } else { |
45 |
| - match[i][j] = false; |
46 |
| - } |
| 17 | + for (int i = s.length() - 1; i >= 0; i--) { |
| 18 | + for (int j = p.length() - 1; j >= 0; j--) { |
| 19 | + if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '?') { |
| 20 | + match[i][j] = match[i + 1][j + 1]; |
| 21 | + } else if (p.charAt(j) == '*') { |
| 22 | + match[i][j] = match[i + 1][j] || match[i][j + 1]; |
| 23 | + } else { |
| 24 | + match[i][j] = false; |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + return match[0][0]; |
47 | 29 | }
|
48 |
| - } |
49 |
| - return match[0][0]; |
50 | 30 | }
|
51 |
| - } |
52 | 31 | }
|
0 commit comments