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. jquery indexOf使用方法

    当无法确定在某个字符串中是否确实存在一个字符的时候,就可调用 indexOf() 和 lastIndexOf() 方法 indexOf() 和 lastIndexOf() 是js的用法,与jquery ...

  2. JSP页面中 <base href="<%=basePath%>">

    base标记是一个基链接标记,是一个单标记.用以改变文件中所有连结标记的参数内定值.它只能应用于标记<head>与</head>之间.你网页上的所有相对路径在链接时都将在前面加 ...

  3. bzoj3555 企鹅QQ

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1640  Solved: 613 Description P ...

  4. TCP/IP详解 学习二

    链路层: 在 T C P / I P协议族中,链路层主要有三个目的:(1)为 I P模块发送和接收 I P数据报:( 2)为 A R P模块发送 A R P请求和接收 A R P应答:( 3)为 R ...

  5. 数字证书文件格式(cer和pfx)的区别

    作为文件形式存在的证书一般有这几种格式: 1.带有私钥的证书 由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进制格式的证书形 ...

  6. POJ1088滑雪(dp+记忆化搜索)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 86411   Accepted: 32318 Description ...

  7. resharper安装后,一不小心点错了(选择了object browser)

    打开Resharper,选择Options,然后选择Tools中的External Sources,你的情况是选择了Navigation to Object Brower这一项了,换成第一个Defau ...

  8. FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MINI-BATCH LEARNING. WHAT IS THE DIFFERENCE?

    FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MIN ...

  9. sencha touch list(列表)、 store(数据源)、model(模型)详解

    //求职 Ext.define('app.model.Staff', { extend: 'Ext.data.Model', config: { fields: [{ name: 'id', type ...

  10. 5个常用Java代码混淆器 助你保护你的代码

    [IT168 技术文档] 从事Java编程的人都知道,可以通过逆向工程反编译得到Java程序的源代码,这种反编译工具之一就是JAD.因此,为保护我们的劳动成果,尽可能给反编译人员制造障碍,我们可以使用 ...