[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 ...
随机推荐
- 将mysql的查询结果导出为csv
要将mysql的查询结果导出为csv,一般会使用php连接mysql执行查询,将返回的查询结果使用php生成csv格式再导出. 但这样比较麻烦,需要服务器安装php才可以实现. 直接使用mysql导出 ...
- 解决eclipse+MAVEN提示One or more constraints have not been satisfied.的问题
应用版本:eclipse luna4.4.1 JDK:1.8 Maven:3.2.5 问题现象: 1.编译工程后总该是显示下面两个错误: One or more constraints have no ...
- Netbeans 配置英文和默认utf-8编码
JAVA SDK 下载地址 http://www.oracle.com/technetwork/java/javase/downloads 解决办法: 进入netbeans 6.5\etc ,其中ne ...
- JS 保留小数点后面2位小数
1. 最笨的办法....... [我就怎么干的.........] function get(){ var s = 22.127456 + ""; var str = ...
- jQuery获取Select选择的Text和 Value(转)
radio: radio: var item = $('input[name=items][checked]').val(); var item = $('input[name=items]:chec ...
- Mobile first! Wijmo 5 + Ionic Framework之:Hello World!
本教程中,我们用Wijmo 5 和 Ionic Framework实现一个Mobile的工程:Hello World. Ionic是什么? Ionic是一个HTML5框架.免费.开源,用于帮助生成hy ...
- 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. 解题思路: ...
- codeforces A. The Wall 解题报告
题目链接:http://codeforces.com/problemset/problem/340/A 这道题目理解不难,就是在[a, b]区间内,找出同时能够被x和y整除的个数.第一次想当然的开了两 ...
- KMP模式匹配_2
http://blog.csdn.net/lin_bei/article/details/1252686 三. 怎么求串的模式值next[n] 定义: (1)next[0]= -1 意义:任何串的第一 ...
- 基数排序(radix sort)
#include<iostream> #include<ctime> #include <stdio.h> #include<cstring> #inc ...