RE-RegularExpresstion规则化表达
原题如下:
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 lettersa-z
.p
could be empty and contains only lowercase lettersa-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 precedeng 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
对于匹配的理解:
Example1:P=a,意味着s中只能是a;
Example2:P=a*,意味着s可以是任意个a,包括0个a;
Example3:P='.*',*代表是对.的重复,而不是对aaaa的重复;
Example4:P='c*a*b',代表s可以是c任意个+a任意个+b,这里的任意个都可以包括0个,但必须有一个b;
Example5:P='mis*is*p*.',mis*是对s的任意重复,而不是对mis的任意重复。