Leetcode 10: Regular Expression Matching

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 10: Regular Expression Matching相关的知识,希望对你有一定的参考价值。

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).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

 1 public class Solution {
 2     public bool IsMatch(string s, string p)
 3     {
 4         if (s == null || p == null)
 5         {
 6             return s == null && p == null;
 7         }
 8 
 9         return IsMatchInternal(s, p, 0, 0);
10     }
11 
12     private bool IsMatchInternal(string source, string pattern, int sStart, int pStart)
13     {
14         if (sStart >= source.Length || pStart >= pattern.Length)
15         {
16             if (sStart >= source.Length && pStart >= pattern.Length)
17             {
18                 return true;
19             }
20 
21             if (pStart >= pattern.Length)
22             {
23                 return false;
24             }
25         }
26 
27         if (pStart < pattern.Length - 1)
28         {
29             if (pattern[pStart + 1] == *)
30             {
31                 if (IsMatchInternal(source, pattern, sStart, pStart + 2)) return true;
32 
33                 if (pattern[pStart] == .)
34                 {
35                     for (int i = sStart; i < source.Length; i++)
36                     {
37                         if (IsMatchInternal(source, pattern, i + 1, pStart + 2)) return true;
38                     }
39                 }
40                 else
41                 {
42                     for (int i = sStart; i < source.Length && source[i] == pattern[pStart]; i++)
43                     {
44                         if (IsMatchInternal(source, pattern, i + 1, pStart + 2)) return true;
45                     }
46                 }
47             }
48             else if (pattern[pStart + 1] == +)
49             {
50                 if (pattern[pStart] == .)
51                 {
52                     for (int i = sStart; i < source.Length; i++)
53                     {
54                         if (IsMatchInternal(source, pattern, i + 1, pStart + 2)) return true;
55                     }
56                 }
57                 else
58                 {
59                     for (int i = sStart; i < source.Length && source[i] == pattern[pStart]; i++)
60                     {
61                         if (IsMatchInternal(source, pattern, i + 1, pStart + 2)) return true;
62                     }
63                 }
64             }
65             else if (sStart < source.Length && (pattern[pStart] == . || pattern[pStart] == source[sStart]))
66             {
67                 if (IsMatchInternal(source, pattern, sStart + 1, pStart + 1)) return true;
68             }
69         }
70         else if (sStart < source.Length && (pattern[pStart] == . || pattern[pStart] == source[sStart]))
71         {
72             if (IsMatchInternal(source, pattern, sStart + 1, pStart + 1)) return true;
73         }
74 
75         return false;
76     }
77 }

 



以上是关于Leetcode 10: Regular Expression Matching的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 10. Regular Expression Matching

leetcode 10. Regular Expression Matching

Leetcode 10: Regular Expression Matching

[LeetCode #10] Regular Expression Matching

[LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

leetcode-10. Regular Expression Matching