44. Wildcard Matching Leetcode Python

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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → falseclass Solution:# @param s, an input string# @param p, a pattern string# @return a boolean## example s = aa p = *# start will maintain the position and i will increasedef isMatch(self, s, p):i = 0j = 0sstar = 0star = -1while i < len(s):## first case compare ? or whether they are exactly the sameif j < len(p) and (s[i] == p[j] or p[j] == '?'):i += 1j += 1## if there is a * in p we mark current j and ielif j < len(p) and p[j] == '*':star = jj += 1sstar = i## if current p[j] is not * we check whether prior state has *elif star != -1:j = star + 1sstar += 1i = sstarelse:return Falsewhile j < len(p) and p[j] == '*':j += 1if j == len(p):return Truereturn False

,快忘了那些不高兴的事吧!你看就连今天的阳光都如此明媚灿烂,

44. Wildcard Matching Leetcode Python

相关文章:

你感兴趣的文章:

标签云: