[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 ... 
随机推荐
- 新浪网易淘宝等IP地区信息查询开放API接口调用方法
			通过IP地址获取对应的地区信息通常有两种方法:1)自己写程序,解析IP对应的地区信息,需要数据库.2)根据第三方提供的API查询获取地区信息. 第一种方法,参见文本<通过纯真IP数据库获取IP地 ... 
- php 二分查找
			<?php /**二分查找:查找一个值在数组中的位置 * @$arr:操作的数组,前提是按顺序排列 * @$val:查找的值 * @$start:查找的起始位置,默认从数组的第一个数找起 * @ ... 
- Linux的文件管理
			绝对路径和相对路径: 绝对路径: /home/tony/Desktop 相对路径:Desktop 或者./Desktop不可写成/Desktop(这是绝对路径的写法) 其中.代表本层目录,..代表上层 ... 
- codeforces  B. Color the Fence  解题报告
			题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ... 
- JavaScript设计模式 - 迭代器模式
			迭代器模式是指提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示. 迭代器模式可以把迭代的过程从业务逻辑中分离出来,在使用迭代器模式之后,即使不关心对象的内部构造,也可以按顺 ... 
- Gym 100801E	Easy Arithmetic (思维题)
			题目:传送门.(需要下载PDF) 题意:给定一个长度不超过1000的字符串表达式,向该表达式中加入'+'或'-',使得表达式的值最大,输出该表达式. 题解:比如300-456就改成300-4+56,遇 ... 
- HTML、CSS选择题
			Java EE软件工程师认证考试 试题库-选择题 一. 选择题(包括单选和双选) 1.D 以下( )是HTML常用的块状标签(选择一项) A. <span> B. < ... 
- HttpHandler简介
			新建的一般处理程序后缀为.ashx,一般会另外新建一个后缀为.ashx.cs的文件,其实所有的代码都写在.ashx.cs里面,只是微软帮我们做了一个傻瓜化的转换新建的一般处理程序,如:Text1,它就 ... 
- 类似于fopen与fopen64的种种情况
			在Linux和unix系统中,我们会遇到¥和¥64的情况.比如stat64,fopen64等 fopen64是linux特有 的,fopen64()函数和fopen()函数相同的,只是底层的文件描述符 ... 
- .net学习笔记----会话状态Session
			一.会话状态Session Session用于服务器端状态管理,使用Session之后,每个客户端都可以将实际的数据保存在服务器上,对于每个客户端的数据,将会生成一个对应的唯一的key(保存在客户端) ... 
