algorithm-in-python/string/wildcard_matching.py

59 lines
1.4 KiB
Python
Raw Normal View History

#coding: utf-8
''' mbinary
#######################################################################
# File : wildcard_matching.py
# Author: mbinary
# Mail: zhuheqin1@gmail.com
2019-01-31 12:09:46 +08:00
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-12-13 22:46
# Description:
wild card '*' matches 0 or any chars, and '?' matches any single char.
#######################################################################
'''
'''
idea
dynamic programming
dp[m+1][n+1]: bool
i:n, j:m
dp[j][i] indicates if s[:i+1] matches p[:j+1]
initial: dp[0][0] = True, dp[0][i],dp[j][0] = False
only if p startswith '*', dp[1][0] = True.
if p[j] = '*': dp[j][i] = dp[j-1][i] or dp[j][i-1]
elif p[j] = '?': dp[j][i] = dp[j-1][i-1]
else : dp[j][i] = dp[j-1][i-1] and s[i] == p[j]
'''
# leetcode: q44 https://leetcode.com/problems/wildcard-matching/description/
2020-04-15 12:28:20 +08:00
def isMatch(self, s, p):
"""
:type s: str
:type p: str pattern str including wildcard
:rtype: bool
"""
2020-04-15 12:28:20 +08:00
n, m = len(s), len(p)
last = [False]*(n+1)
last[0] = True
for j in range(m):
2020-04-15 12:28:20 +08:00
if p[j] == '*':
for i in range(n):
last[i+1] = last[i+1] or last[i]
2020-04-15 12:28:20 +08:00
elif p[j] == '?':
last.pop()
2020-04-15 12:28:20 +08:00
last.insert(0, False)
else:
li = [False]
for i in range(n):
2020-04-15 12:28:20 +08:00
li.append(last[i] and p[j] == s[i])
last = li
return last[-1]