LeetCode 44. Wildcard Matching
2016-11-13 15:55
429 查看
Problem Statement
(Source) 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
Tags:
Dynamic Programming,
Backtracking,
Greedy,
String.
Solution 1 - Backtracking
class Solution(object): def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ n, m = len(s), len(p) i, j, next_match, star = 0, 0, 0, -1 while i < n: if j < m and (s[i] == p[j] or p[j] == '?'): i, j = i+1, j+1 elif j < m and p[j] == '*': star = j j += 1 next_match = i elif star != -1: next_match += 1 i = next_match j = star + 1 else: return False while j < m and p[j] == '*': j += 1 return j == m
Reference:
(1) https://discuss.leetcode.com/topic/3040/linear-runtime-and-constant-space-solution
相关文章推荐
- LeetCode 44 Wildcard Matching
- LeetCode 44. Wildcard Matching--动态规划
- LeetCode-44-Wildcard Matching DP
- leetcode 10|44. Regular Expression Matching 44. Wildcard Matching
- leetcode 44 Wildcard Matching
- leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法
- LeetCode 44. Wildcard Matching(通配符匹配)
- 【LeetCode】44. Wildcard Matching解法及注释
- [leetcode]44. Wildcard Matching(Java)
- LeetCode_44---Wildcard Matching
- LeetCode(44) Wildcard Matching
- [LeetCode] 44. Wildcard Matching 外卡匹配
- Leetcode 44. Wildcard Matching
- 44. Wildcard Matching Leetcode Python
- leetcode 44: Wildcard Matching
- LeetCode 44. Wildcard Matching(模糊匹配)
- [LeetCode]44. Wildcard Matching 深入浅出讲解和代码示例
- [Leetcode] 44. Wildcard Matching
- LeetCode 44 Wildcard Matching
- LeetCode 44 Wildcard Matching(字符串匹配问题)