May 22, 2020 10:30 Technology
This is a LeetCode hard question solved by dynamic programming (DP) algorithm.
Given an input string (s
) and a pattern (p
), 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).
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 = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:
Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:
Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
Example 5:
Input:
s = "mississippi"
p = "mis*is*p*."
Output: false
class Solution:
def isMatch(self, s: 'str', p: 'str') -> 'bool':
if len(p) == 0:
if len(s) == 0: return True
else: return False
else:
if len(s) == 0:
# if all the even locations are *, return True
if (''.join(set(p[1::2])) == '*') and p[-1]=='*': return True
else: return False
else:
np = len(p); ns = len(s)
flag = [[False for j in range(ns+1)] for i in range(np+1)]
# first row: p=''
flag[0][0] = True # s='', p=''
flag[0][1:] = [False for j in range(ns)] # s='xxx', p=''
# first column: s=''
for i in range(1, np+1):
if i%2==0 and p[i-1]=='*':
flag[i][0] = flag[i-2][0]
for j in range(1, ns+1):
for i in range(1, np+1):
if (p[i-1]=='*') and ( (flag[i][j-1] and (p[i-2] in [s[j-1], '.'])) or flag[i-2][j] ):
flag[i][j] = True
if (p[i-1] in ['.', s[j-1]]) and flag[i-1][j-1]:
flag[i][j] = True
return flag[-1][-1]