题目:通配符匹配

难度:hard

题目内容

Given an input string (s) and a pattern (p), 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).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like ? or *.

翻译:给定一个输入字符串(s)和一个模版(p),实现通配符模式匹配,并支持“?””和“*”。

”?匹配任何一个字符。

“匹配任何字符序列(包括空序列)。

匹配应该覆盖整个输入字符串(而不是部分)。

 

注意:

s可以是空的,并且只包含小写字母a-z。

p可以是空的,只包含小写字母a-z,还有?或*。

我的思路:此题和第10题很像:Regular Expression Matching,但是10题里是' . '和 ' * ',这里是' ? ' 和 ‘ * ’,

' . '和 ' ? ' 意义相对应,但是  ' * ' 表示意义不同,第10题是只代表个数,本题是代表任何序列,于是把第十题代码修改下:

1、first_match要加上 pattern.charAt(0) == '*' 的情况;

2、后面分两种互斥情况A、B:

A:以 * 匹配:(此时又分两种相容情况)

   a1:*没有匹配任何的字符

   a2:*匹配上此字符

  A情况的结果为  a1 || a2

B:没*:

  当前匹配结果 && 下一个元素和下一个pattern的匹配结果。

我的代码:

     public boolean isMatch(String text, String pattern) {
if (pattern.isEmpty()) return text.isEmpty();
boolean first_match = (!text.isEmpty() &&
(pattern.charAt(0) == text.charAt(0) || pattern.charAt(0) == '?' || pattern.charAt(0) == '*')); if (pattern.charAt(0) == '*'){
return (isMatch(text, pattern.substring(1)) ||
(isMatch(text.substring(1), pattern)));
} else {
return first_match && isMatch(text.substring(1), pattern.substring(1));
}
}

结果:1614 / 1808 test cases passed.

Last executed input:   "aaabbbaabaaaaababaabaaabbabbbbbbbbaabababbabbbaaaaba"   "a*******b"

------Time Limit Exceeded------

看来那个方法是可行的,可能是因为用了递归,中间太多***导致子递归太多,所以运行太慢。

答案代码

     public boolean isMatch(String s, String p) {
int m = s.length(), n = p.length();
char[] sc = s.toCharArray();
char[] pc = p.toCharArray();
boolean[][] dp = new boolean[m + 1][n + 1];
dp[0][0] = true;
for(int j = 1; j < n + 1; j++){
if(pc[j - 1] == '*') dp[0][j] = dp[0][j - 1];
} for(int i = 1; i < m + 1; i++){
for(int j = 1; j < n + 1; j++){
if(pc[j - 1] == sc[i - 1] || pc[j - 1] == '?'){
dp[i][j] = dp[i - 1][j - 1];
} else if(pc[j - 1] == '*'){
dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
} else {
dp[i][j] = false;
}
}
}
return dp[m][n];
}

答案复杂度:O(M*N)

答案思路:使用一个二维数组对每个点的匹配状态进行记录,后面点的匹配状态取决于当前匹配和之前匹配,所以最后一个点的匹配状态就是整个匹配结果。

数组的行列分别为pattern,str

算法主要分两部分:

1、给第0行和第0列赋值:

  第0列——除了第一个为true以外都为默认值false;

  第0行——如果pattern某个位置为*,则此时对应第0行的值等于前面一个的值,否则取默认值false。

2、从第第1行第1列的元素开始:

  如果当前str值与pattern的值直接匹配上(或者匹配?)则二维数组此值直接取上一个“元素”的匹配结果:【i-1】【j-1】

  如果当前str值与pattern的值是靠‘’*‘’才匹配上,则二维数组此值等于

    (*没有匹配上此字符 || *匹配上此字符)==(这个元素和上一个pattern结果 || 这一个pattern和上一个元素的结果)

  以上两者都不是,则取默认值false

eg. pattern = *a**a    str = bbaa

bp[][]=   ' '       *      a       *      *       a

'' [true, true, false, false, false, false]
b [false, true, false, false, false, false]
b [false, true, false, false, false, false]
a [false, true, true, true, true, false]
a [false, true, true, true, true, true]

结论:第二种(答案)方法虽然不错,但是理解起来太麻烦,不过也很好记,记住思路也行。。。

   在一般的面试或者笔试中使用第一种(我写的)方法足以应对。

LeetCode第[44]题(Java):Wildcard Matching的更多相关文章

  1. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  2. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. [Leetcode 44]通配符匹配Wildcard Matching

    [题目] 匹配通配符*,?,DP动态规划,重点是*的两种情况 想象成两个S.P长度的字符串,P匹配S. S中不会出现通配符. [条件] (1)P=null,S=null,TRUE (2)P=null, ...

  6. LeetCode第[10]题(Java):Regular Expression Matching

    题目:匹配正则表达式 题目难度:hard 题目内容:Implement regular expression matching with support for '.' and '*'. '.' Ma ...

  7. leetcode第十题--Regular Expression Matching

    Problem:Implement regular expression matching with support for '.' and '*'. '.' Matches any single c ...

  8. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  9. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

随机推荐

  1. crash处理core文件

    (一时心血来潮总结的,供大家参考,时间仓促,不足之处勿拍砖,欢迎讨论~)Crash工具用于解析Vmcore文件,Vmcore文件为通过kdump等手段收集的操作系统core dump信息,在不采用压缩 ...

  2. 亚马逊 MWS 开发者指南 漏桶算法

    流量控制与令牌桶算法|James Pan's Blog  https://blog.jamespan.me/2015/10/19/traffic-shaping-with-token-bucket 服 ...

  3. Character Sets, Collation, Unicode :: utf8_unicode_ci vs utf8_general_ci

    w Hi, You can check and compare sort orders provided by these two collations here: http://www.collat ...

  4. 模块化之SeaJS(一)

    模块化(之SeaJS) 刚接触的童鞋可能会有很多疑惑,比喻:什么是模块?模块的目的是干嘛呀?怎么样实现模块化呢? 不要急,博主正是带着这三个问题来写这篇文章的. 一,什么是模块化? 在前端开发领域,一 ...

  5. Java 面向对象编程介绍

    面向对象的概念 类与对象的关系 封装 面向对象 面向过程: 强调的是过程(动作) 面向对象: 强调的是对象(实体) 面向对象的特点 面向对象就是一种常见的思想,符合人们的思考习惯; 面向对象的出现,将 ...

  6. Delphi日期设置为NULL

    在某些情况下,需要将日期字段的值置空,这种操作比较麻烦.在操作的时候,可将变量的值(t:TDateTime)设置为0,在操作的过程中进行判断,当t的值为0或-1的时候,时间值为1899年的Delphi ...

  7. java 使用LinkedList模拟一个堆栈或者队列数据结构

    近期在复习下java基础,看了下java基础,在看到集合时突然发现想起来曾经面试有一道笔试题:模拟一个堆栈或者队列数据结构,当时还没做出来,今天就写一下,首先得明确堆栈和队列的数据结构 堆栈:先进后出 ...

  8. spring 整合mybatis找不到${jdbc.driverClass}

    1.检查是否设置了mapper扫描org.mybatis.spring.mapper.MapperScannerConfigurer类 在spring里使用org.mybatis.spring.map ...

  9. datetime时间处理

    基本数据获取: In [38]: import datetime as dt In [39]: on = dt.datetime.now() #获取当前准确时间 In [40]: on Out[40] ...

  10. Python(调用函数、定义函数)

    函数的返回值: return 值:只能返回一次,只要执行return函数就终止 返回值:没有类型限制,也没有个数限制 没有return:None 返回一个值 返回多个值:元组 先定义,后使用,定义阶段 ...