Given an input string (s) and a pattern (p), 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).

Note:

s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.
Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

class Solution {
public boolean isMatch(String s, String p) {
return recur(s,p,0,0);
} public boolean recur(String s, String p, int sPtr, int pPtr) {
if(s.length() == sPtr && p.length() == pPtr) return true;
if(p.length() == pPtr) return false;
if(s.length() == sPtr){
if(p.length() > pPtr+1 && p.charAt(pPtr+1)=='*') return recur(s,p,sPtr,pPtr+2);
else return false;
} if(p.length() > pPtr+1 && p.charAt(pPtr+1)=='*'){ //next bit is *
if(recur(s,p,sPtr,pPtr+2)) return true; //* match 0 element
else{
for(int i = 1; sPtr + i <= s.length() && (p.charAt(pPtr)==s.charAt(sPtr+i-1)|| p.charAt(pPtr)=='.'); i++){
if(recur(s,p,sPtr+i,pPtr+2)) return true; //* match i elements
}
}
}
else if(p.charAt(pPtr)=='.' || p.charAt(pPtr)==s.charAt(sPtr))
return recur(s, p, sPtr+1, pPtr+1); return false;
}
}

当当前字符之后的那个字符是*时,我们需要对当前字符做特别判断,所以没次递归中要判断p字符串的下一个字符是否是*

最新文章

  1. [Xilinx]Modelsim独立仿真Vivado生成的PLL核
  2. SQL Server日期和时间的格式
  3. consul的安装配置 一centos7环境
  4. Multi-source Replication
  5. 深入学习:如何实现不同Android设备之间相同应用程序的网络服务发现功能
  6. 神奇的脱机 app_offline.htm
  7. spring4 定时任务
  8. void指针、NULL指针和未初始化指针
  9. eclipse tomcat内存溢出,加大内存
  10. 关于Mongdb的java的CRUD操作
  11. java返回数据工具类
  12. pycharm中replace的应用
  13. Request method &#39;POST&#39; not supported错误和解决方法
  14. printf scanf cin cout的区别与特征
  15. VUE2.0 饿了吗视频学习笔记(三):VUE2.0取消了v-link
  16. 解释器模式 Interpreter
  17. 基于Windows 机器学习(Machine Learning)的图像分类(Image classification)实现
  18. winform界面开发-HTML内容编辑控件
  19. Atitit.注解解析(1)---------词法分析 attilax总结 java .net
  20. java通过ping 判断网络是否正常

热门文章

  1. js&#183;&#183;事件捕捉
  2. 《Linux内核原理与分析》第三周作业
  3. #考研笔记#计算机之PPT问题
  4. hdu5003 Osu!排序实现水题
  5. SELinux初探
  6. linux子系统搭建python3
  7. PythonStudy——列表类型 List type
  8. 【python】*与**
  9. [SQL]T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)
  10. Centos创建用户