LeetCode 44. Wildcard Matching

简介: 给定输入字符串s和模式串(p),实现通配符模式匹配支持'?' 和'*'.

Description



Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.


python


'?' Matches any single character.

'*' Matches any sequence of characters (including the empty sequence).

The matching should cover the entire input string (not partial).


Note:


  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like ? or *.


Example 1:


Input: s = "aa" p = "a" Output: false

Explanation: "a" does not match the entire string "aa".


Example 2:


Input: s = "aa" p = "" Output: true

Explanation: '' matches any sequence.


Example 3:


Input: s = "cb" p = "?a" Output: false

Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.


Example 4:


Input: s = "adceb" p = "*a*b" Output: true

Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".


Example 5:


Input: s = "acdcb" p = "a*c?b" Output: false


描述



给定输入字符串s和模式串(p),实现通配符模式匹配支持'?' 和'*'.


思路



  • 根据题意?应当和任意一个字符匹配,即?需要匹配且仅匹配一个字符,*可以匹配任意种类字符,任意长度字符,也可以不匹配.
  • 这里的难点在于不清楚*应该匹配多少个字符,匹配过多或者过少都可能倒是后面的匹配失效.
  • 我们使用四个变量indexs,indexp,matcheds,star,其中indexs用来记录s的索引,indexp用来记录模式串p的索引.
  • 如果字符串s[indexs]和模式串p[indexp]相等,或者模式串p[indexp]为?时,我们让indexs和indexp自增一位.
  • 如果字符串s[indexs]和模式串p[indexp]不等,且模式串p[indexp]是*时,我们让star记录下此时*的位置,matcheds记录下此时s[indexs]的位置.
  • 表示p中索引star的*可能需要与字符串s中索引为matched的字符匹配,然后我们置indexp为star+1,indexs保持不变.
  • 如果遇到了s中既不和p中字符相等,且p中的字符不为?和*,我们检查p前面是否有*.
  • 如果存在*,则我们让matched自增一个,表示原来的假设成立,即*需要和matched自增前表示的字符匹配,matched自增表示*可能需要与当前的字符匹配.
  • 如果没有*,说没匹配不成功,直接返回False
  • 遍历完之后,检查p字符串,如果还有剩余:检查剩余的是否全是*,若全是*返回True,否则返回False


class Solution:
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        # 首先获取字符串s和模式串p的长度
        lens, lenp = len(s), len(p)
        indexp, indexs, indexstar, matcheds = 0, 0, -1, 0
        # 循环条件,还没有遍历到s尾部时才继续执行
        while indexs < lens:
            # 当p还没有到达尾部,且s和p对应位置字符相等或p位置字符为?号时
            if indexp < lenp and (s[indexs] == p[indexp] or p[indexp] == '?'):
                # 同时自增,表示相等
                indexs += 1
                indexp += 1
            # 当p还没有到达尾部且s和p对应位置不相等,且p为*号
            elif indexp < lenp and p[indexp] == "*":
                # 记下此时*号的位置
                indexstar = indexp
                # 记下此时*号对应匹配的s中字符的位置,此时还不确定*号是否需要和此字符匹配
                matcheds = indexs
                # 继续匹配
                indexp += 1
            # 当s和p对应位置不等,且p位置不为*,检查前面是否有*号
            elif indexstar != -1:
                # 如果有,则说明前面的*号需要匹配一个字符,matcheds自增一个,indexp指向后面一个,indexs重新开始于matcheds
                matcheds += 1
                indexp = indexstar+1
                indexs = matcheds
            # 如果以上条件都不满足(即不满足s和p对应位置相等,且不满不p前面位置有8号)
            else:
                # 查找失败,返回False
                return False
        # 如果在s遍历完成之后,p还有剩余,则检查p剩余的内容
        while indexp < lenp:
            # 只要p中剩余的内容有一个不是*,返回False
            if p[indexp] != '*':
                return False
            indexp += 1
        return True


源代码文件在这里.

目录
相关文章
LeetCode - 44. Wildcard Matching
44. Wildcard Matching  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个字符串和一个自动机,问你自动机可否接收这个字符串.
1208 0
|
C++
[LeetCode] Wildcard Matching
Well, so many people has tried to solve this problem using DP. And almost all of them get TLE (if you see a C++ DP solution that gets accepted, please let me know ^_^).
1015 0
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
25 6
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
26 4
|
12天前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
37 2
|
12天前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
15 7
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
12 4
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
31 5
|
12天前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
22 3