前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >No.010 Regular Expression Matching

No.010 Regular Expression Matching

作者头像
mukekeheart
发布2018-02-27 12:00:13
5670
发布2018-02-27 12:00:13
举报

10. Regular Expression Matching

  • Total Accepted: 89193
  • Total Submissions: 395441
  • Difficulty: Hard

Implement regular expression matching with support for '.' and '*'.

代码语言:javascript
复制
'.' 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


思路:
代码语言:javascript
复制
参考自:http://www.tuicool.com/articles/Ebiymu
  • 首先要理解题意:
    • "a"对应"a", 这种匹配不解释了
    • 任意字母对应".", 这也是正则常见
    • 0到多个相同字符x,对应"x*", 比起普通正则,这个地方多出来一个前缀x. x代表的是 相同的字符中取一个,比如"aaaab"对应是"a*b"
    • "*"还有一个易于疏忽的地方就是它的"贪婪性"要有一个限度.比如"aaa"对应"a*a", 代码逻辑不能一路贪婪到底
  • 正则表达式如果期望着一个字符一个字符的匹配,是非常不现实的.而"匹配"这个问题,非 常容易转换成"匹配了一部分",整个匹配不匹配,要看"剩下的匹配"情况.这就很好的把 一个大的问题转换成了规模较小的问题:递归
  • 确定了递归以后,使用java来实现这个问题,会遇到很多和c不一样的地方,因为java对字符 的控制不像c语言指针那么灵活charAt一定要确定某个位置存在才可以使用.
  • 如果pattern是"x*"类型的话,那么pattern每次要两个两个的减少.否则,就是一个一个 的减少. 无论怎样减少,都要保证pattern有那么多个.比如s.substring(n), 其中n 最大也就是s.length()
代码语言:javascript
复制
 1 public boolean isMatch(String s, String p) {
 2     // base case
 3     if (p.length() == 0) {
 4         return s.length() == 0;
 5     }
 6  
 7     // special case
 8     if (p.length() == 1) {
 9         // if the length of s is 0, return false
10         if (s.length() < 1) {
11             return false;
12         }
13         //if the first does not match, return false
14         else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
15             return false;
16         }
17  
18         // otherwise, compare the rest of the string of s and p.
19         else {
20             return isMatch(s.substring(1), p.substring(1));
21         }
22     }
23  
24     // case 1: when the second char of p is not '*'
25     if (p.charAt(1) != '*') {
26         if (s.length() < 1) {
27             return false;
28         }
29         if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
30             return false;
31         } else {
32             return isMatch(s.substring(1), p.substring(1));
33         }
34     }
35  
36     // case 2: when the second char of p is '*', complex case.
37     else {
38         //case 2.1: a char & '*' can stand for 0 element
39         if (isMatch(s, p.substring(2))) {
40             return true;
41         }
42  
43         //case 2.2: a char & '*' can stand for 1 or more preceding element, 
44         //so try every sub string
45         int i = 0;
46         while (i<s.length() && (s.charAt(i)==p.charAt(0) || p.charAt(0)=='.')){
47             if (isMatch(s.substring(i + 1), p.substring(2))) {
48                 return true;
49             }
50             i++;
51         }
52         return false;
53     }
54 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-07-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 10. Regular Expression Matching
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档