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. 【CodeForces 472A】Design Tutorial: Learn from Math

    题 题意:给你一个大于等于12的数,要你用两个合数表示出来.//合数指自然数中除了能被1和本身整除外,还能被其他的数整除(不包括0)的数. 分析:我们知道偶数除了2都是合数,给你一个偶数,你减去一个偶 ...

  2. 第七节 JBPM 中的脚本语言

    1.JPDL表达式 2.动作:数据库操作例子 3.路由:transaction一个流程之间的指向 4.BeanShell脚本语言 例子: 发布到数据库中才能做一个测试类

  3. 学习笔记 BIT(树状数组)

    痛定思痛,打算切割数据结构,于是乎直接一发BIT 树状数组能做的题目,线段树都可以解决 反之则不能,不过树状数组优势在于编码简单和速度更快 首先了解下树状数组: 树状数组是一种操作和修改时间复杂度都是 ...

  4. 常用sql,在做项目时用mysqlWorkBeach里面自动生成的

    -- 修改表中的字段的长度ALTER TABLE `sfkbbs`.`sfk_father_module` CHANGE ) NULL DEFAULT NULL COMMENT '父板块名字' ; 在 ...

  5. MAC OS下安装Erlang

    这是个很大的问题,也是个很小的问题,恩.因为我最终解决方案是用了别人写的工具,名叫erlbrew: https://github.com/mrallen1/erlbrew 按照页面上的指导安装使用即可 ...

  6. poj1787Charlie's Change(多重背包+记录路径+好题)

    Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3720   Accepted: 1125 ...

  7. android经典实战项目视频教程下载

    注:这是一篇转载的文章,原文具体链接地址找不到了,将原文分享如下,希望能对看到的朋友有所帮助! 最近在学习android应用方面的技术,自己在网上搜集了一些实战项目的资料,感觉挺好的,发布出来跟大伙分 ...

  8. textView中判断文本长度,自定义表情长度为1,emoj表情长度为1,输入限制

      static const int MAX_LIMIT_NUMS = 100; /**< 输入个数限制 */ // self.inputNumberTipsLabel 控制器的view上一个用 ...

  9. python 入门

    bool t, f = True, False print type(t) # Prints "<type 'bool'>"   字符串 hello = 'hello' ...

  10. Openresty 与 Tengine

    Openresty 与 Tengine Openresty和Tengine基于 Nginx 的两个衍生版本,某种意义上他们都和淘宝有关系,前者是前淘宝工程师agentzh主导开发的,后者是淘宝的一个开 ...