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 第一遍:
public class Solution {
public boolean isMatch(String s, String p) {
if(p.length() == 0) return s.length() == 0;
if(s.length() == 0) return p.length() == 0;
if(p.charAt(0) == '?' || p.charAt(0) == s.charAt(0)) return isMatch(s.substring(1), p.substring(1));
else if(p.charAt(0) == '*'){
for(int i = 0; i < s.length(); i ++){
if(isMatch(s.substring(i), p.substring(1))) return true;
}
return false;
}
else return false;
}
}
Time Limit Exceeded
"abbabbbaabaaabbbbbabbabbabbbabbaaabbbababbabaaabbab", "*aabb***aa**a******aa*"
网上做法:
贪心的策略,能匹配就一直往后遍历,匹配不上了就看看前面有没有'*'来救救场,再从'*'后面接着试。
public class Solution {
public boolean isMatch(String s, String p) {
int i = 0;
int j = 0;
int star = -1;
int mark = -1;
while (i < s.length()){
if (j < p.length() && (p.charAt(j) == '?' || p.charAt(j) == s.charAt(i))) {
++i;
++j;
} else if (j < p.length() && p.charAt(j) == '*') {
star = j++;
mark = i;
} else if (star != -1) {
j = star + 1;
i = ++mark;
} else {
return false;
}
}
while (j < p.length() && p.charAt(j) == '*') {// i == s.length()
++j;
}
return j == p.length();
}
}
DP 解法: 但是会memory limit exceeded:
public class Solution {
public boolean isMatch(String s, String p) {
if(p == null || p.length() == 0) return s == null || s.length() == 0;
if(s == null || s.length() == 0){
return p == null || p.length() == 0 || (p.charAt(0) == '*' && isMatch(s, p.substring(1)));
}
int plen = p.length();
int slen = s.length();
boolean[][] dp = new boolean[plen][slen];
if(p.charAt(0) == s.charAt(0) || p.charAt(0) == '?' || p.charAt(0) == '*') dp[0][0] = true;
for(int i = 1; i < plen; i ++){
if(p.charAt(i) == '*') dp[i][0] = dp[i - 1][0];
else break;
}
for(int j = 1; j < slen; j ++){
if(p.charAt(0) == '*') dp[0][j] = dp[0][j - 1];
}
for(int i = 1; i < plen; i ++){
for(int j = 1; j < slen; j ++){
if(p.charAt(i) == '?' || p.charAt(i) == s.charAt(j)) dp[i][j] = dp[i - 1][j - 1];
else if(p.charAt(i) == '*'){
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - 1] || dp[i][j - 1];
}else{
dp[i][j] = false;
}
}
}
return dp[plen - 1][slen - 1];
}
}
Wildcard Matching的更多相关文章
- 【leetcode】Wildcard Matching
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- 44. Wildcard Matching
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- [OJ] Wildcard Matching (Hard)
LintCode 192. Wildcard Matching (Hard) LeetCode 44. Wildcard Matching (Hard) 第二次刷还是被这题虐. 其实就是跪在一个地方, ...
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
- [LeetCode] Wildcard Matching 题解
6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...
- Regular Expression Matching & Wildcard Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- LeetCode: Wildcard Matching 解题报告
Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...
- [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching
开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...
随机推荐
- jquery的prop()和attr()
jQuery1.6以后prop()和attr()的应用场景如下: 第一原则:只添加属性名称该属性就会立即生效应该使用prop(); 第二原则:只存在true/false的属性应该使用prop(); 设 ...
- 安装SRILM
参考博文:Ubuntu 64位系统下SRILM的配置详解 来源52nlp www.52nlp.cn 首先下载SRILM 解压缩到home即可 然后需要修改MakeFile文件: # SRILM = / ...
- Supporting Connected Routes to Subnet Zero
Supporting Connected Routes to Subnet Zero IOS allows the network engineer to tell a router to eithe ...
- IE浏览器各版本的CSS Hack
IE浏览器各版本的CSS Hack 如下示例: .test{ color:black;/*W3C*/ color:red\9;/* IE6-IE10 */ _color:black;/*IE6*/ ...
- spring使用JdbcDaoSupport中封装的JdbcTemplate进行query
1.Dept package cn.hxex.springcore.jdbc; public class Dept { private Integer deptNo; private String d ...
- DataReader 链接关闭解惑篇
不管是啥xxDataReader,都是继承DataReader实现的,所以是有共性的,因此标题就以DataReader为题了. 情况一:DataReader 默认链接不关闭 static void M ...
- c++基础(二):成员he派生类
struct Date{ int day, month, year; }; struct Book{ string name; Date date; void init(string n, int y ...
- 自定义Drawable
本文由 伯乐在线 - treesouth 翻译,toolate 校稿.未经许可,禁止转载! 英文出处:ryanharter.com.欢迎加入翻译小组. 我们看过一些博客文章,讲述了为什么要适时地使用自 ...
- [转]ubuntu错误解决E: Sub-process /usr/bin/dpkg returned an error code (1)
[转]ubuntu错误解决E: Sub-process /usr/bin/dpkg returned an error code (1) http://yanue.net/post-123.html ...
- iOS 取得单张系统图片
这里主要用到了UIImagePickerController 不多废话,直接上代码 // // RootViewController.m // GetImageFromPhotoAlbum // // ...