Implement wildcard pattern matching with support for '?' and '*'.

  1. '?' Matches any single character.
  2. '*' Matches any sequence of characters (including the empty sequence).
  3.  
  4. The matching should cover the entire input string (not partial).
  5.  
  6. The function prototype should be:
  7. bool isMatch(const char *s, const char *p)
  8.  
  9. Some examples:
  10. isMatch("aa","a") false
  11. isMatch("aa","aa") true
  12. isMatch("aaa","aa") false
  13. isMatch("aa", "*") true
  14. isMatch("aa", "a*") true
  15. isMatch("ab", "?*") true
  16. isMatch("aab", "c*a*b") false

  17. 解题思路一:
    参考之前写的Java for LeetCode 010 Regular Expression Matching,可以很轻松的用递归写出代码,JAVA实现如下:
  1. static public boolean isMatch(String s, String p) {
  2. if(s.length()==0){
  3. for(int i=0;i<p.length();i++)
  4. if(p.charAt(i)!='*')
  5. return false;
  6. return true;
  7. }
  8. if (p.length() == 0)
  9. return s.length() == 0;
  10. else if (p.length() == 1)
  11. return p.charAt(0)=='*'||(s.length() == 1&& (p.charAt(0) == '?' || s.charAt(0) == p.charAt(0)));
  12. if(p.charAt(0)!='*'){
  13. if(p.charAt(0)!=s.charAt(0)&&p.charAt(0)!='?')
  14. return false;
  15. return isMatch(s.substring(1),p.substring(1));
  16. }
  17. int index=0;
  18. while(index<p.length()){
  19. if(p.charAt(index)=='*')
  20. index++;
  21. else break;
  22. }
  23. if(index==p.length())
  24. return true;
  25. p=p.substring(index);
  26. for(int i=0;i<s.length();i++){
  27. if(isMatch(s.substring(i),p))
  28. return true;
  29. }
  30. return false;
  31. }

结果Time Limit Exceeded!也就是说这种类似暴力枚举的算法肯定不能通过!

解题思路二:

用两个指针indexS和indexP遍历s和p,同时用两个指针starIndex和sPosition来记录*遍历过程中*最后一次出现的位置和对应的indexP的位置,一旦s和p不匹配,就回到starIndex的位置,同时sPosition+1,接着遍历,直到遍历完整个s为止,注意边界条件,JAVA实现如下:
  1. static public boolean isMatch(String s, String p) {
  2. int indexS=0,indexP=0,starIndex=-2,sPosition=-2;
  3. while(indexS<s.length()){
  4. if(starIndex==p.length()-1)
  5. return true;
  6. if(indexP>=p.length()){
  7. if(starIndex<0)
  8. return false;
  9. indexP=starIndex+1;
  10. indexS=++sPosition;
  11. }
  12. if(s.charAt(indexS)==p.charAt(indexP)||p.charAt(indexP)=='?'){
  13. indexS++;
  14. indexP++;
  15. }
  16. else if(p.charAt(indexP)=='*'){
  17. starIndex=indexP++;
  18. sPosition=indexS;
  19. }
  20. else if(starIndex>=0){
  21. indexP=starIndex+1;
  22. indexS=++sPosition;
  23. }
  24. else return false;
  25. }
  26. while(indexP<p.length()){
  27. if(p.charAt(indexP)!='*')
  28. return false;
  29. indexP++;
  30. }
  31. return true;
  32. }

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. Spring JdbcTemplate 的使用与学习(转)

    紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...

  2. phpMyadmin /scripts/setup.php Remote Code Injection && Execution CVE-2009-1151

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Insufficient output sanitizing when gener ...

  3. Mybatis的ResultMap的使用

    本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...

  4. IOS基础之 (七) 分类Category

    一 Category 分类:Category(类目,类别) (OC有) 命名:原来的类+类别名(原来的类名自动生成,只要写后面的类别名,一般以模块名为名.比如原来类 Person,新建分类 Ct,新建 ...

  5. Aop 是面向切面编程,

    Aop 是面向切面编程,是在业务代码中可以织入其他公共代码(性能监控等),现在用普通的方法实现AOP http://blog.csdn.net/heyanfeng22/article/details/ ...

  6. linux中软链接和硬链接的区别与小结

    ln命令 该命令在文件之间创建链接.这种操作实际上是给系统中已有的某个文件指定另外一个可用于访问它的名称.对于这个新的文件名,我们可以为之指定不同的访问权限,以控制对信息的共享和安全性的问题. 如果链 ...

  7. 慎用 Enum.GetHashCode()

    公司里遗留下了相当多的 Enum.GetHashCode()来获取枚举值的代码 但是这会产生装箱行为的!!因为Enum是值类型,GetHashCode()是Object的方法,调用GetHashCod ...

  8. 利用dedecms autoindex让文章列表加上序列号

    有些时候我们在制作模板的需要在文章标题前面加上序列号,可以通过织梦自带的autoindex属性来实现,实现方法很简单,只需要在序号递增的地方加上 这段代码就行,[field:global runphp ...

  9. [LeetCode] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  10. VIM、GVIM在WINDOWS下中文乱码的终极解决方案

    文章转自:http://www.liuhuadong.com/archives/68 vim.gvim在windows下中文乱码的终极解决方案在windows下vim的中文字体显示并不好,所以我们需要 ...