Wildcard Matching

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).

Example

isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false


分析:

这题也是DP问题,横轴是S, 纵轴是P(含有?和*),那么我们可以得到:

if (p.charAt(i - 1) == s.charAt(j - 1) || p.charAt(i - 1) == ‘?‘) {
  match[i][j] = match[i - 1][j - 1];
} else if (p.charAt(i - 1) == ‘*‘) {
  match[i][j] = match[i - 1][j - 1] || match[i - 1][j] || match[i][j - 1];

  // match[i][j - 1] 指的是用* 替换S中1个j或多个j之前的character,当然那些character可以是连续的。
}

 1 public class Solution {
 2     /**
 3      * @param s: A string
 4      * @param p: A string includes "?" and "*"
 5      * @return: A boolean
 6      */
 7     public boolean isMatch(String s, String p) {
 8         if (s == null || p == null)
 9             return false;
10         boolean[][] match = new boolean[p.length() + 1][s.length() + 1];
11         match[0][0] = true;
12         for (int i = 1; i < match[0].length; i++) {
13             match[0][i] = false;
14         }
15
16         for (int i = 1; i < match.length; i++) {
17             if (p.charAt(i - 1) == ‘*‘) {
18                 match[i][0] = match[i - 1][0];
19             }
20         }
21
22         for (int i = 1; i < match.length; i++) {
23             for (int j = 1; j < match[0].length; j++) {
24                 if (p.charAt(i - 1) == s.charAt(j - 1) || p.charAt(i - 1) == ‘?‘) {
25                     match[i][j] = match[i - 1][j - 1];
26                 } else if (p.charAt(i - 1) == ‘*‘) {
27                     match[i][j] = match[i - 1][j - 1] || match[i - 1][j] || match[i][j - 1];
28                 }
29             }
30         }
31
32         return match[p.length()][s.length()];
33     }
34 }
时间: 2024-10-05 21:09:41

Wildcard Matching的相关文章

【leetcode】Wildcard Matching

Wildcard Matching 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 partia

LeetCode: Wildcard Matching [043]

[题目] 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 funct

[LintCode] Wildcard Matching

Wildcard Matching 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

[LeetCode][JavaScript]Wildcard Matching

Wildcard Matching 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 partia

LeetCode 044 Wildcard Matching

题目要求:Wildcard Matching 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 p

[LeetCode]wildcard matching通配符实现之贪心法

前天用递归LTE,昨天用动态规划LTE,今天接着搞,改用贪心法.题目再放一次: '?'匹配任意字符,'*'匹配任意长度字符串 Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*"

[LeetCode]wildcard matching通配符实现之动态规划

前天wildcard matching没AC,今天接着搞,改用动态规划.题目再放一次: '?'匹配任意字符,'*'匹配任意长度字符串 Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*&q

[LeetCode]wildcard matching通配符实现TLE

leetcode这道题还挺有意思的,实现通配符,'?'匹配任意字符,'*'匹配任意长度字符串,晚上尝试了一下,题目如下: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching shoul

【LeetCode】Wildcard Matching 串匹配 动态规划

题目:Wildcard Matching <span style="font-size:18px;">/*LeetCode WildCard matching * 题目:给定一个目标串和一个匹配串,判定是否能够匹配 * 匹配串中的定义:字符---->字符,*---->0个.1个或者多个字符,?------>对应任意一个字符 * 思路:动态规划:*:dp[i][j] = dp[i][j-1] || dp[i-1][j] * ? || s[i] == p[i]

Regular Expression Matching &amp; Wildcard Matching

Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The