You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:
53
+
54
+
55
+
'.' Matches any single character.
56
+
'*' Matches zero or more of the preceding element.
57
+
58
+
59
+
The matching should cover the entire input string (not partial).
60
+
61
+
62
+
Example 1:
63
+
64
+
65
+
Input: s = "aa", p = "a"
66
+
Output: false
67
+
Explanation: "a" does not match the entire string "aa".
68
+
69
+
70
+
Example 2:
71
+
72
+
73
+
Input: s = "aa", p = "a*"
74
+
Output: true
75
+
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
76
+
77
+
78
+
Example 3:
79
+
80
+
81
+
Input: s = "ab", p = ".*"
82
+
Output: true
83
+
Explanation: ".*" means "zero or more (*) of any character (.)".
84
+
85
+
86
+
87
+
Constraints:
88
+
89
+
90
+
1 <= s.length <= 20
91
+
1 <= p.length <= 20
92
+
s contains only lowercase English letters.
93
+
p contains only lowercase English letters, '.', and '*'.
94
+
It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
You can perform the following process on s any number of times:
55
+
56
+
57
+
Choose an index i in the string such that there is at least one character to the left of index i that is equal to s[i], and at least one character to the right that is also equal to s[i].
58
+
Delete the closest occurrence of s[i] located to the left of i.
59
+
Delete the closest occurrence of s[i] located to the right of i.
60
+
61
+
62
+
Return the minimum length of the final string s that you can achieve.
63
+
64
+
65
+
Example 1:
66
+
67
+
68
+
Input: s = "abaacbcbb"
69
+
70
+
Output: 5
71
+
72
+
Explanation:
73
+
We do the following operations:
74
+
75
+
76
+
Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = "bacbcbb".
77
+
Choose index 3, then remove the characters at indices 0 and 5. The resulting string is s = "acbcb".
78
+
79
+
80
+
81
+
Example 2:
82
+
83
+
84
+
Input: s = "aa"
85
+
86
+
Output: 2
87
+
88
+
Explanation:
89
+
We cannot perform any operations, so we return the length of the original string.
0 commit comments