Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence). 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", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false

解题思路一:
参考之前写的Java for LeetCode 010 Regular Expression Matching,可以很轻松的用递归写出代码,JAVA实现如下:
static public boolean isMatch(String s, String p) {
if(s.length()==0){
for(int i=0;i<p.length();i++)
if(p.charAt(i)!='*')
return false;
return true;
}
if (p.length() == 0)
return s.length() == 0;
else if (p.length() == 1)
return p.charAt(0)=='*'||(s.length() == 1&& (p.charAt(0) == '?' || s.charAt(0) == p.charAt(0)));
if(p.charAt(0)!='*'){
if(p.charAt(0)!=s.charAt(0)&&p.charAt(0)!='?')
return false;
return isMatch(s.substring(1),p.substring(1));
}
int index=0;
while(index<p.length()){
if(p.charAt(index)=='*')
index++;
else break;
}
if(index==p.length())
return true;
p=p.substring(index);
for(int i=0;i<s.length();i++){
if(isMatch(s.substring(i),p))
return true;
}
return false;
}

结果Time Limit Exceeded!也就是说这种类似暴力枚举的算法肯定不能通过!

解题思路二:

用两个指针indexS和indexP遍历s和p,同时用两个指针starIndex和sPosition来记录*遍历过程中*最后一次出现的位置和对应的indexP的位置,一旦s和p不匹配,就回到starIndex的位置,同时sPosition+1,接着遍历,直到遍历完整个s为止,注意边界条件,JAVA实现如下:
static public boolean isMatch(String s, String p) {
int indexS=0,indexP=0,starIndex=-2,sPosition=-2;
while(indexS<s.length()){
if(starIndex==p.length()-1)
return true;
if(indexP>=p.length()){
if(starIndex<0)
return false;
indexP=starIndex+1;
indexS=++sPosition;
}
if(s.charAt(indexS)==p.charAt(indexP)||p.charAt(indexP)=='?'){
indexS++;
indexP++;
}
else if(p.charAt(indexP)=='*'){
starIndex=indexP++;
sPosition=indexS;
}
else if(starIndex>=0){
indexP=starIndex+1;
indexS=++sPosition;
}
else return false;
}
while(indexP<p.length()){
if(p.charAt(indexP)!='*')
return false;
indexP++;
}
return true;
}

Java for LeetCode 044 Wildcard Matching的更多相关文章

  1. LeetCode 044 Wildcard Matching

    题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...

  2. 【leetcode】Wildcard Matching

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  3. LeetCode - 44. Wildcard Matching

    44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...

  4. [LeetCode] 44. Wildcard Matching 外卡匹配

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  5. 【leetcode】Wildcard Matching(hard) ★ 大神太牛了

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  6. [leetcode]44. Wildcard Matching万能符匹配

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  7. LeetCode题解-----Wildcard Matching

    题目描述: '?' Matches any single character. '*' Matches any sequence of characters (including the empty ...

  8. LeetCode 44 Wildcard Matching(字符串匹配问题)

    题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description   '?' Matches any single chara ...

  9. 044 Wildcard Matching 通配符匹配

    实现一个支持 '?' 和 '*' 的通配符匹配.'?' 匹配任何单个字符.'*' 匹配任何数量的字符 (包括0个).匹配应覆盖 整个 输入字符串(而不是部分).这个函数原型为:bool isMatch ...

随机推荐

  1. POJ1201 Intervals

    Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...

  2. asp.net 实现在线打印功能,jQuery打印插件PrintArea实现自动分页

    使用的组件:jQuery打印插件PrintArea,有兴趣的可以研究一下. 使用方法略过,这里将介绍如何实现打印多页是可以分页. 现在提供两种方法思路: 1.根据特定的打印机型号和使用的纸张类型,然后 ...

  3. [NOIP2010] 提高组 洛谷P1525 关押罪犯

    刚才做并查集想到了这道以前做的题,干脆一并放上来 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可 ...

  4. x86汇编指令详解

    80x86指令系统 80x86指令系统,指令按功能可分为以下七个部分. (1) 数据传送指令. (2) 算术运算指令. (3) 逻辑运算指令. (4) 串操作指令. (5) 控制转移指令. (6) 处 ...

  5. ssh项目删除

    1.在页面添加删除跳转 <td><a href="deleteStandard.action?id=${sta.id }">删除</a>< ...

  6. B/S C/S架构的界面测试

    网站是B/S架构的典型,从做网站的有限经验来整理一下B/S测试的基本要点,并把它与C/S进行区分. 与C/S相比,以下4个测试是除了常用测试外还要注意的: (1)链接测试 (2)表单测试 (3)脚本测 ...

  7. 让VisualVM+BTrace进入unsafe mode

    让VisualVM+BTrace进入unsafe mode http://kenai.com/projects/btrace/pages/UserGuide BTrace很强大,但有很多安全限制,比如 ...

  8. ios框架中UIResponder的职责链设计模式应用

    今天有空,就把UIResponder的职责链图上传一下 如果不理解职责链设计模式的朋友,请参考:http://www.cnblogs.com/langtianya/p/4060941.html

  9. Jquery 获取URL参数

    使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下 1.window.location.href; 其实只是用到了javas ...

  10. 简单的分页存储过程,Json格式日期转换为一般日期

    简单的分页存储过程 CREATE PROC Paged @pageIndex INT, @pageCount INT OUTPUT, @pageSize INT AS DECLARE @count I ...