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 第一遍:
 public class Solution {
public boolean isMatch(String s, String p) {
if(p.length() == 0) return s.length() == 0;
if(s.length() == 0) return p.length() == 0;
if(p.charAt(0) == '?' || p.charAt(0) == s.charAt(0)) return isMatch(s.substring(1), p.substring(1));
else if(p.charAt(0) == '*'){
for(int i = 0; i < s.length(); i ++){
if(isMatch(s.substring(i), p.substring(1))) return true;
}
return false;
}
else return false;
}
}

Time Limit Exceeded

"abbabbbaabaaabbbbbabbabbabbbabbaaabbbababbabaaabbab", "*aabb***aa**a******aa*"

网上做法:

贪心的策略,能匹配就一直往后遍历,匹配不上了就看看前面有没有'*'来救救场,再从'*'后面接着试。

 public class Solution {
public boolean isMatch(String s, String p) {
int i = 0;
int j = 0;
int star = -1;
int mark = -1;
while (i < s.length()){
if (j < p.length() && (p.charAt(j) == '?' || p.charAt(j) == s.charAt(i))) {
++i;
++j;
} else if (j < p.length() && p.charAt(j) == '*') {
star = j++;
mark = i;
} else if (star != -1) {
j = star + 1;
i = ++mark;
} else {
return false;
}
}
while (j < p.length() && p.charAt(j) == '*') {// i == s.length()
++j;
}
return j == p.length();
}
}

DP 解法: 但是会memory  limit exceeded:

 public class Solution {
public boolean isMatch(String s, String p) {
if(p == null || p.length() == 0) return s == null || s.length() == 0;
if(s == null || s.length() == 0){
return p == null || p.length() == 0 || (p.charAt(0) == '*' && isMatch(s, p.substring(1)));
}
int plen = p.length();
int slen = s.length();
boolean[][] dp = new boolean[plen][slen];
if(p.charAt(0) == s.charAt(0) || p.charAt(0) == '?' || p.charAt(0) == '*') dp[0][0] = true;
for(int i = 1; i < plen; i ++){
if(p.charAt(i) == '*') dp[i][0] = dp[i - 1][0];
else break;
}
for(int j = 1; j < slen; j ++){
if(p.charAt(0) == '*') dp[0][j] = dp[0][j - 1];
}
for(int i = 1; i < plen; i ++){
for(int j = 1; j < slen; j ++){
if(p.charAt(i) == '?' || p.charAt(i) == s.charAt(j)) dp[i][j] = dp[i - 1][j - 1];
else if(p.charAt(i) == '*'){
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - 1] || dp[i][j - 1];
}else{
dp[i][j] = false;
}
}
}
return dp[plen - 1][slen - 1];
}
}
 

Wildcard Matching的更多相关文章

  1. 【leetcode】Wildcard Matching

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

  2. LeetCode - 44. Wildcard Matching

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

  3. 44. Wildcard Matching

    题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...

  4. [OJ] Wildcard Matching (Hard)

    LintCode 192. Wildcard Matching (Hard) LeetCode 44. Wildcard Matching (Hard) 第二次刷还是被这题虐. 其实就是跪在一个地方, ...

  5. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

  6. [LeetCode] Wildcard Matching 题解

    6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...

  7. Regular Expression Matching & Wildcard Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  8. 【LeetCode】44. Wildcard Matching (2 solutions)

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

  9. LeetCode: Wildcard Matching 解题报告

    Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...

  10. [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching

    开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...

随机推荐

  1. WebApp JS 打开 app

    产品需求:分享出去的链接比如到微信朋友圈,微博的H5页面,添加一个按钮 open App 用来打开并启动自己公司的APP (如果当前手机已经安装自己公司的APP) 废话少说直接上代码: <inp ...

  2. ode.js 版本控制 nvm 和 n 使用 及 nvm 重启终端失效的解决方法

    今天的话题包括2个部分 node.js 下使用 nvm 或者 n 来进行版本控制 nvm 安装node.js 版本后,重启终端 node , npm 环境变量失效 第一部分 用什么来管理 node.j ...

  3. hdu 1196 Lowest Bit

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1196 Lowest Bit Description Given an positive integer ...

  4. Go中简单的文件读写

    Go中的ioutil包可以方便的实现文件读写.代码: package main import ( "fmt" "io/ioutil" ) func main() ...

  5. 006--VS2013 C++ 加载其他格式图片,并显示半透明化

    //--------------------------------------------MyPaint() 函数------------------------------------------ ...

  6. ultraedit高亮显示设置

    ultraedit高亮显示设置 | 浏览:2333 | 更新:2014-02-20 23:05 1 2 3 4 5 6 7 分步阅读 百度经验:jingyan.baidu.com 写代码的人对ultr ...

  7. P1297: [SCOI2009]迷路

    首先知道,如果没有路径长度的要求,且给定的邻接矩阵只有0和1表示通与不通的话,从S->E走N次的方案数就是这个矩阵自乘N次后的(S,E)的数值.这样的话只需要快速幂+矩阵乘法即可过关. (转载请 ...

  8. Ubuntu14.04搭建LAMP环境

    安装Apache2                                                             sudo apt-get install apache2   ...

  9. Teamwork-Week2真对必应词典和有道词典的软件分析和用户需求调查(桌面版)

    经调查,现在有道词典在该领域拥有很大程度的市场占有率,所以我们将有道词典与必应词典进行对比. 核心功能一:单词本 有道词典中的单词本都只能是由用户手动添加不会的单词,而必应词典中的单词 不仅可以被自己 ...

  10. 用时间复杂度为n的方法找出水王

    一.题目       三人行设计了一个灌水论坛.信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子.坊间风闻该“水王”发帖 数目超过了帖子数目 ...