Regular Expression Matching leetcode java
题目:
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
题解:
本文及代码引用自:http://harrifeng.github.io/algo/leetcode/regular-expression-matching.html
- 首先要理解题意:
- "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()
代码如下:
1 public static boolean isMatch(String s, String p) {
2 if (p.length() == 0)
3 return s.length() == 0;
4
5 // length == 1 is the case that is easy to forget.
6 // as p is subtracted 2 each time, so if original
7 // p is odd, then finally it will face the length 1
8 if (p.length() == 1)
9 return (s.length() == 1) && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.');
10
11 // next char is not '*': must match current character
12 if (p.charAt(1) != '*') {
13 if (s.length() == 0)
14 return false;
15 else
16 return (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')
17 && isMatch(s.substring(1), p.substring(1));
18 }else{
19 // next char is *
20 while (s.length() > 0 && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.')) {
21 if (isMatch(s, p.substring(2)))
22 return true;
23 s = s.substring(1);
24 }
25 return isMatch(s, p.substring(2));
26 }
27 }
2 if (p.length() == 0)
3 return s.length() == 0;
4
5 // length == 1 is the case that is easy to forget.
6 // as p is subtracted 2 each time, so if original
7 // p is odd, then finally it will face the length 1
8 if (p.length() == 1)
9 return (s.length() == 1) && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.');
10
11 // next char is not '*': must match current character
12 if (p.charAt(1) != '*') {
13 if (s.length() == 0)
14 return false;
15 else
16 return (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')
17 && isMatch(s.substring(1), p.substring(1));
18 }else{
19 // next char is *
20 while (s.length() > 0 && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.')) {
21 if (isMatch(s, p.substring(2)))
22 return true;
23 s = s.substring(1);
24 }
25 return isMatch(s, p.substring(2));
26 }
27 }
分类:
leetcode每题整理
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架