|
1 | 1 | # -*- coding:utf-8 -*-
|
2 | 2 |
|
3 | 3 |
|
4 |
| -# Implement regular expression matching with support for '.' and '*'. |
| 4 | +# Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. |
5 | 5 | #
|
6 | 6 | #
|
7 |
| -# '.' Matches any single character. |
8 |
| -# '*' Matches zero or more of the preceding element. |
| 7 | +# '.' Matches any single character. |
| 8 | +# '*' Matches zero or more of the preceding element. |
| 9 | +# |
9 | 10 | #
|
10 | 11 | # The matching should cover the entire input string (not partial).
|
11 | 12 | #
|
12 |
| -# The function prototype should be: |
13 |
| -# bool isMatch(const char *s, const char *p) |
14 |
| -# |
15 |
| -# Some examples: |
16 |
| -# isMatch("aa","a") → false |
17 |
| -# isMatch("aa","aa") → true |
18 |
| -# isMatch("aaa","aa") → false |
19 |
| -# isMatch("aa", "a*") → true |
20 |
| -# isMatch("aa", ".*") → true |
21 |
| -# isMatch("ab", ".*") → true |
22 |
| -# isMatch("aab", "c*a*b") → true |
| 13 | +# Note: |
| 14 | +# |
| 15 | +# |
| 16 | +# s could be empty and contains only lowercase letters a-z. |
| 17 | +# p could be empty and contains only lowercase letters a-z, and characters like . or *. |
| 18 | +# |
| 19 | +# |
| 20 | +# Example 1: |
| 21 | +# |
| 22 | +# |
| 23 | +# Input: |
| 24 | +# s = "aa" |
| 25 | +# p = "a" |
| 26 | +# Output: false |
| 27 | +# Explanation: "a" does not match the entire string "aa". |
| 28 | +# |
| 29 | +# |
| 30 | +# Example 2: |
| 31 | +# |
| 32 | +# |
| 33 | +# Input: |
| 34 | +# s = "aa" |
| 35 | +# p = "a*" |
| 36 | +# Output: true |
| 37 | +# Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". |
| 38 | +# |
| 39 | +# |
| 40 | +# Example 3: |
| 41 | +# |
| 42 | +# |
| 43 | +# Input: |
| 44 | +# s = "ab" |
| 45 | +# p = ".*" |
| 46 | +# Output: true |
| 47 | +# Explanation: ".*" means "zero or more (*) of any character (.)". |
| 48 | +# |
| 49 | +# |
| 50 | +# Example 4: |
| 51 | +# |
| 52 | +# |
| 53 | +# Input: |
| 54 | +# s = "aab" |
| 55 | +# p = "c*a*b" |
| 56 | +# Output: true |
| 57 | +# Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". |
| 58 | +# |
| 59 | +# |
| 60 | +# Example 5: |
| 61 | +# |
| 62 | +# |
| 63 | +# Input: |
| 64 | +# s = "mississippi" |
| 65 | +# p = "mis*is*p*." |
| 66 | +# Output: false |
| 67 | +# |
23 | 68 | #
|
24 | 69 |
|
25 | 70 |
|
|
0 commit comments