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 Solution 1:
贪心算法:
http://blog.unieagle.net/2012/11/07/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Awildcard-matching/ 只需要依据连续的’*’,将p分割成一些不带’*’的子串。然后在s中依次匹配就好了,只不过需要特殊照顾一下首尾两个子串:
1.对于开头不是’*’的p,第一个子串必须从s[0]开始匹配
2.对于结尾不是’*’的p,最后一个子串必须在s的尾巴上匹配
 package POJ;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; public class Main { public static void main(String[] args) {
Main so = new Main();
String s="AABCDEFGHIJKLMNOPQ";
String p="AAB*Q**Q";
System.out.println(so.isMatch(s, p));
} public boolean isMatch(String s, String p) {
if(s == null || p == null)
return false;
int front = p.indexOf("*"); //得到第一个*的位置,也即得到第一个*前边儿的字符个数
int back = p.length()-1-p.lastIndexOf("*"); //得到最后一个*后边儿还有多少个位数
if(front == -1){
//p中没有*
return (s.length()==p.length())&&(iMatch(s,p));
}
//p中有*
//首先,确定首尾是能满足条件的
if(!( (front+back<=s.length())&&(iMatch(s.substring(0, front),p.substring(0, front)))&&(iMatch(s.substring(s.length()-back),p.substring(p.length()-back)))))
return false; int i1=0;
int i2=0; //现在来确定首尾的两个*中间的部分,还是以*来作为分割,一段一段地看
while(true){
while((i2<p.length())&&(p.charAt(i2)=='*'))
++i2;
if(i2==p.length())
break;
int st=i2;
while((i2<p.length())&&(p.charAt(i2)!='*'))
++i2;
String piece=p.substring(st,i2); //找到被*分割的片段
while(((i1+piece.length())<=s.length())&&!iMatch(s.substring(i1, i1+piece.length()),piece))
i1++;
if(i1+piece.length()>s.length())
return false;
i1=i1+piece.length();
}
return true;
} private boolean iMatch(String s, String p) {
// TODO Auto-generated method stub
for(int i=0;i<s.length();++i){
if(!((s.charAt(i)==p.charAt(i))||(p.charAt(i)=='?')))
return false;
}
return true;
}
}
												

[Leetcode] Wildcard Matching的更多相关文章

  1. LeetCode: Wildcard Matching 解题报告

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

  2. [LeetCode] Wildcard Matching 题解

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

  3. [LeetCode] Wildcard Matching 外卡匹配

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  4. [leetcode]Wildcard Matching @ Python

    原题地址:https://oj.leetcode.com/problems/wildcard-matching/ 题意: Implement wildcard pattern matching wit ...

  5. [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  6. [Leetcode] Wildcard matching 通配符匹配

    Implement wildcard pattern matching with support for'?'and'*'. '?' Matches any single character. '*' ...

  7. leetcode Wildcard Matching greedy algrithm

    The recursive program will result in TLE like this: class Solution { public: bool isMatch(const char ...

  8. [LeetCode]Wildcard Matching 通配符匹配(贪心)

    一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...

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

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

随机推荐

  1. (转)女生应该找一个玩ACM的男生

    1.强烈的事业心 将来,他也一定会有自己热爱的事业.而且,男人最性感的时刻之一,就是他专心致志做事的时候.所以,找一个机会在他全神贯注玩ACM的时候,从侧面好好观察他,你就会发现我说的话没错. 2.永 ...

  2. 【GoLang】GoLang fmt 占位符详解

    golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. # 定义示例类型和变量 type Human struct { Name string } var peo ...

  3. 【云计算】Netflix 开源持续交付平台 Spinnaker

    oschina        发布于: 2015年11月19日 (0评)          分享到:    收藏 +1 CDS首都在线全球云主机.全球私有网络,开工送礼,免费试用! »   日前,Ne ...

  4. css 属性

    部分属性在firefox 中添加-moz- safari 以及chrome  加上-webkit- opera 加上-o- ie9里可能需要-ms- jquery  中的css  操作  和一般的cs ...

  5. python 列表转为字典的两个小方法

    1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...

  6. 14.约瑟夫环问题[JosephusProblem]

    [题目] n个数字(0,1,…,n-1)形成一个圆圈,从数字0开始,每次从这个圆圈中删除第m个数字(第一个为当前数字本身,第二个为当前数字的下一个数字).当一个数字删除后,从被删除数字的下一个继续删除 ...

  7. iOS 中使用Block时需要注意的retain circle

    现在在ios中,block是越来越多了.自己在类中定义block对象时,需要注意block对象的使用方法,防止产生retain circle,导致内存泄露. 现在分析一下产生retain circle ...

  8. 在eclipse中进行Struts2项目的配置

    Struts2是一个比较出色的基于MVC设计模式的框架,是由Struts1和WebWork发展而来的,性能也比较稳定,现在是Apache软件基金会的一个项目,下面就来配置Struts2进行初始化的开发 ...

  9. Java for LeetCode 073 Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解题思路: ...

  10. HDU 1087 Super Jumping! Jumping! Jumping! 最大递增子序列

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...