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 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) {
       const char * ptr;
       const char *str;
       bool start = false;
       for(ptr=p,str=s; *str!='\0'  ; ){
           switch(*ptr){
               case '?':
               str++,ptr++;
               break;
               case '*':
               start = true;
               while(*ptr == '*')
                 ptr++;
               if(*ptr=='\0')
               return true;
               p = ptr;
               s = str;
               break;
               default:
               if(*ptr != *str){
                   if(!start)
                   return false;
                   ptr = p;
                   str = s+1;
                   s=s+1;
               }
              else{
                  ptr++;
                  str++;
              }
           }
       }
       while(*ptr=='*')  ptr++;

       return (*ptr == '\0');
    }
};

Leetcode_Wildcard Matching,布布扣,bubuko.com

时间: 2024-08-08 17:32:29

Leetcode_Wildcard Matching的相关文章

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

No matching bean of type dao found for depende

场景: Spring MVC + MyBatis 启动服务失败,控制台打印一系统异常. 异常大意如下: xxxService 加载 xxxInterface 或 xxxMapper 失败 最后一条异常 No matching bean of type [xxxService] found for depende 思考: 从异常本身看:找不到依赖或加载错误的接口很早以前就已经存在了,所以不会有问题 所以从最新修改或新增的接口上查找问题.定位到  xxxService 解决: 通常 No match

Stable Matching Problem

The Stable Matching Problem originated, in part, in 1962, when David Gale and Lloyd Shapley, two mathematical economists, asked the question: Could one design a college admissions process, or a job recruiting process, that was self-enforcing? What di

leetcode-44. Wildcard Matching

这道题和之前的那道Regular Expression Matching有点相似,第一反应是跟之前一样,用递归来做 bool doMatch(char *s,char *p) { if (*p == '\0') return *s == '\0'; if(*p == '*') { while(*s!='\0')//通配符匹配0次,1次···· { if(doMatch(s,p+1)) return true; s++; } //通配符匹配完后还是无法匹配 return doMatch(s,p+1

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 p

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

【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】Regular Expression Matching (hard) ★

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 function prototype should be

【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