10. Regular Expression Matching (JAVA)
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字符串的下一个字符是否是*
10. Regular Expression Matching (JAVA)的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [eetcode 10]Regular Expression Matching
1 题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
随机推荐
- 2018.4.27 java容器
一.容器的概念 在Java当中,如果有一个类专门用来存放其它类的对象,这个类就叫做容器,或者就叫做集合,集合就是将若干性质相同或相近的类对象组合在一起而形成的一个整体 二.容器与数组的关系 之所以需要 ...
- C# 比较两个路径是否指向同一对象
string path1 = @"c:\test\rootpath"; string path2 = @"C:\TEST\..\TEST\ROOTPATH"; ...
- What’s New In GRANDstack?
转自:https://blog.grandstack.io/whats-new-in-grandstack-310c067fea4a There’s been a lot of activity in ...
- JAVA工程师面试题库
这些都是从其他地方copy过来的,如有侵权的话,可以联系我下架.这期只有问题,后面我会整理答案再重新发出来. http://blog.csdn.net/jackfrued/article/detail ...
- CMake for MFC example
cmake_minimum_required(VERSION 2.8) add_definitions(-D_AFXDLL) set(CMAKE_MFC_FLAG 2) set(SRCS MFCApp ...
- socket资源
http://www.360doc.com/content/13/1231/16/14919052_341525862.shtml Linux下基于socket多线程并发通信的实现 https://w ...
- VS2010+WPF+LINQ for MySQL
学习wpf,连接数据库和linq for mysql 1.参考以前博文,恢复在 Vs2010+linQ for Mysql的环境. 2.建立 wpf工程,参照1,生成 datacontext.cs , ...
- django路由系统URLS
usrls: from django.contrib import admin from django.urls import path from cmbd import views from dja ...
- 【python】self & cls
转自 python中self,cls 普通的方法,第一个参数需要是self,它表示一个具体的实例本身. 如果用了staticmethod,那么就可以无视这个self,而将这个方法当成一个普通的函数使用 ...
- [转] SQL日期函数dayadd/datediff/datepart
函数一: CREATE OR REPLACE FUNCTION dayadd(p_Component varchar2, p_Number number, p_Date date) RETURN DA ...