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. Python脚本控制的WebDriver 常用操作 <七>浏览器前进和后退操作

    下面将使用WebDriver来控制浏览器的前进和后退操作 测试用例场景 此操作和get.url()方法功能相同 Python脚本 # coding=gbk ''' Created on 2013年12 ...

  2. C语言实现冒泡排序法和选择排序法代码参考

    为了易用,我编写排序函数,这和直接在主调函数中用是差不多的. 我认为选择排序法更好理解!请注意 i 和 j ,在写代码时别弄错了,不然很难找到错误! 冒泡排序法 void sort(int * ar, ...

  3. CF 191 总结

    A. Flipping Game 链接:http://codeforces.com/contest/327/problem/A 题意:从 i 到 j 翻转一次使得 1 的 个数最多~ 直接暴力搞~ # ...

  4. Mybatis关于like的字符串模糊处理

    其中通过"%"#{key}"%"来拼接语句 <sql id="select_where"> from cellphone c l ...

  5. <转载>编程珠玑-位排序(bitsort)

    转载:http://www.cnblogs.com/shuaiwhu/archive/2011/05/29/2065039.html  维护版权   在<编程珠玑>一书上,有一题是将一堆不 ...

  6. Object C学习笔记2-NSLog 格式化输出数据

    1 . 几种常用类型变量声明 int i =10; BOOL isShow=YES; BOOL isShow=1; float f = 3.1415926; char a =120; NSString ...

  7. SQL-LINQ-Lambda语法对照,好记性不如烂笔头

    忘记的时候就翻阅翻阅吧~~ SQL LINQ Lambda SELECT *FROM HumanResources.Employee from e in Employees select e Empl ...

  8. EF6 在原有数据库中使用 CodeFirst 总复习(一、搭建基础环境)

    本来以为已经会了,可动手时发现许多问题还是模糊不清,正所谓眼高手低.只能重新查资料,再复习一遍. vs.net2013 ef6 mvc5 sqlserver2008   一.建立数据库  Bloggi ...

  9. 关于SVN 目录结构,使用教程

    SVN使用教程:http://www.cnblogs.com/armyfai/p/3985660.html Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn ...

  10. PHP笔记-PHP中Web Service.

    这几天工作需要.net站点免登陆访问PHP的Wiki站点. PHP不熟,感觉很苦逼.任务下来了,必须搞定.准备用SSO,太麻烦了,要改写别人很多代码,这个是第三方CMS,封装的很厉害,不好改.最后我的 ...