首页 > 其他 > 详细

Leetcode 10: Regular Expression Matching

时间:2018-01-08 14:31:12      阅读:172      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/liangmou/p/8242124.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!