[Leetcode] Wildcard Matching
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的更多相关文章
- LeetCode: Wildcard Matching 解题报告
Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...
- [LeetCode] Wildcard Matching 题解
6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [leetcode]Wildcard Matching @ Python
原题地址:https://oj.leetcode.com/problems/wildcard-matching/ 题意: Implement wildcard pattern matching wit ...
- [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [Leetcode] Wildcard matching 通配符匹配
Implement wildcard pattern matching with support for'?'and'*'. '?' Matches any single character. '*' ...
- leetcode Wildcard Matching greedy algrithm
The recursive program will result in TLE like this: class Solution { public: bool isMatch(const char ...
- [LeetCode]Wildcard Matching 通配符匹配(贪心)
一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
随机推荐
- django 1.7 新特性 --- data migration
官方文档:https://docs.djangoproject.com/en/dev/topics/migrations/ 1.7 之前大家可能会用south用于管理数据库的模型的同步.1.7之后dj ...
- BZOJ 1600
开始刷一些USACO月赛题了.. 这题简单递推就不说了. 然后我们发现暴力递推是$O(n^2)$的.看起来非常慢. 这道题拥有浓厚的数学色彩,因此我们可以从数学它的规律上找突破口. (于是暴力大法好, ...
- php方法 隐藏手机号中间四位
$num = "13966778888"$str = substr_replace($num,'****',3,4);
- 编译 uImage
编译 uImage 和测试 u-Boot - 小猪爱拱地 - 博客频道 - CSDN.NET http://blog.csdn.net/caspiansea/article/details/38057 ...
- Sharepoint 2010 创建栏 计算栏
SharePoint 创建栏时,可以添加计算字段, 网上查了查,相关资料如下: http://wenku.baidu.com/view/936239e9b8f67c1cfad6b88f.html ht ...
- 57. 数对之差的最大值:4种方法详解与总结[maximum difference of array]
[本文链接] http://www.cnblogs.com/hellogiser/p/maximum-difference-of-array.html [题目] 在数组中,数字减去它右边的数字得到一个 ...
- 46. 对称子字符串的最大长度(ToDo)
[题目] 输入一个字符串,输出该字符串中对称的子字符串的最大长度.比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. [分析] 可能很多人都写过判断一个字符串 ...
- iOS 定制controller过渡动画 ViewController Custom Transition使用体会
最近学习了一下ios7比较重要的一项功能,就是 controller 的 custom transition. 在ios7中,navigation controller 中就使用了交互式过渡来返回上级 ...
- 如何用OpenCV自带的adaboost程序训练并检测目标
参考博文: 1.http://blog.csdn.net/wuxiaoyao12/article/details/39227189 2.http://www.cnblogs.com/easymind2 ...
- MVC学习笔记---MVC的处理管线
漫步ASP.NET MVC的处理管线 ASP.NET MVC从诞生到现在已经好几个年头了,这个框架提供一种全新的开发模式,更符合web开发本质.你可以很好的使用以及个性化和扩展这个框架,但这需要你 ...