LeetCode 44: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

题目要求实现模糊匹配,代码如下:

class Solution {
public:
    bool isMatch(const char *s, const char *p) {
    	bool isStar = false;
		const char* s1, *p1;
		while(*s && (*p || isStar)){
			if((*s==*p) || (*p=='?')){
				s++; p++;
			}else if(*p == '*'){
				isStar = true;
				p++;
				if(*p=='\0') return true;
				s1 = s;
				p1 = p;
			}else{
				if(!isStar) return false;
				p =p1;
				s = ++s1;
			}
		}
		if(*s=='\0'){
			while(*p=='*')p++;
			if(*p=='\0') return true;

		}
		return false;

    }
};
时间: 2024-08-06 03:47:27

LeetCode 44:Wildcard Matching的相关文章

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 fu

每日算法之三十五: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 en

【剑指offer】19、正则表达式匹配 && 【Leetcode】44、Wildcard Matching

题目一 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式.例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配 思路 用动态规划来解决 规定 若,则str[0~i-1]和pattern[0~j-1]匹配 1.当p[j

天题系列: Wildcard Matching

题解抄自“http://simpleandstupid.com/2014/10/26/wildcard-matching-leetcode-%E8%A7%A3%E9%A2%98%E7%AC%94%E8%AE%B0/” “ Implement wildcard pattern matching with support for ‘?’ and ‘*’. ‘?’ Matches any single character.‘*’ Matches any sequence of characters (

leetcode044: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 funct

【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 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 44.Wildcard Matching (通配符匹配) 解题思路和方法

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,

第八周 Leetcode 44. Wildcard Matching 水题 (HARD)

Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分匹配完成就算贪心了吧.. class Solution { public: bool match(string &s,string &p,int l2,int r2,int l1) { if(l2==r2)return true; if(l1+r2-l2-1>=s.length())re