码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 10: Regular Expression Matching

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

标签:port   prototype   code   ons   gpo   with   har   return   char   

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

标签:port   prototype   code   ons   gpo   with   har   return   char   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!