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

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
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

类似于Regular Expression Matching,会超时

 1 class Solution {
 2 public:
 3     bool isMatch(const char *s, const char *p) {
 4
 5         if(*s==‘\0‘&&*p==‘\0‘) return true;
 6         if(*s!=‘\0‘&&*p==‘\0‘) return false;
 7         if(*s==‘\0‘&&*p!=‘\0‘) return false;
 8
 9         if(*p==‘*‘)
10         {
11             while(*p==‘*‘) p++;
12
13
14             while(*s!=‘\0‘)
15             {
16                 if(isMatch(s,p)) return true;
17                 s++;
18             }
19
20    return isMatch(s,p);
21         }
22         else if(*p==‘?‘)
23         {
24             return isMatch(s+1,p+1);
25         }
26         else
27         {
28             return (*s==*p&&isMatch(s+1,p+1));
29         }
30
31
32         return false;
33     }
34 };

采用类似回溯的思想

依次对字符进行比对,如果发现p==‘*‘

则从(p+1)开始和s开始依次比较,如果发现不对,

则回溯到(p+1)的位置,再从s+1开始依次比较

如果再不对,则再次回溯到(p+1)的位置,从s+2位置开始依次比较

……

 1 class Solution {
 2 public:
 3     bool isMatch(const char *s, const char *p) {
 4
 5
 6         const char* afterStarPositionP=NULL;
 7         const char* afterStarPositionS=NULL;
 8         while(*s!=‘\0‘)
 9         {
10             if(*s==*p||*p==‘?‘)
11             {
12                 s++;p++;
13                 continue;
14             }
15             else if(*p==‘*‘)
16             {
17                 p++;
18                 afterStarPositionP=p;
19                 afterStarPositionS=s;
20             }
21             else
22             {
23                 if(afterStarPositionP!=NULL)
24                 {
25                     afterStarPositionS++;
26                     s=afterStarPositionS;
27                     p=afterStarPositionP;
28                 }
29                 else
30                 {
31                     return false;
32                 }
33             }
34         }
35
36         while(*p==‘*‘)
37         {
38             p++;
39         }
40
41         if(*p==‘\0‘) return true;
42         else return false;
43     }
44 };
时间: 2024-10-30 08:18:24

【leetcode】Wildcard Matching的相关文章

【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]

【leetcode】Wildcard Matching(hard) ★ 大神太牛了

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 p

【LeetCode】【C++】Wildcard Matching

题目 '?' 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)

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【LeetCode】贪心 greedy(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: [55]Jump Game (2018年11月27日,算法群) 给了一个数组nums,nums[i] = k 代表站在第 i 个位置的情况下, 我最多能往前走 k 个单

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

【leetcode刷题笔记】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). The function p

【leetcode】44. Wildcard Matching

题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹配string[0:j] ,如果pattern[i] == string[j] 或者 pattern[i] == '?',那么dp[i][j]  = dp[i-1][j-1]:如果pattern[i] = '*' 则复杂一些,因为'*'可以匹配s[j-1],s[j] 或者不匹配,因此只要满足其中任意