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次). 另一种是通配符匹配,我们在 ...
随机推荐
- CheckBox和RadioButton
多选按钮CheckBox的使用方法和常用的监听器:OnClickListener.OnCheckedChangeListener 在activity_main.xml中使用LinearLayout布局 ...
- 线程操作API
线程操作API 1.currentThread 2.getId() .getName().getPriority().getStart.isAlive().isDaemon().isInterrupt ...
- 最大似然估计(MLE)和最大后验概率(MAP)
最大似然估计: 最大似然估计提供了一种给定观察数据来评估模型参数的方法,即:“模型已定,参数未知”.简单而言,假设我们要统计全国人口的身高,首先假设这个身高服从服从正态分布,但是该分布的均值与方差未知 ...
- hdu 1890 Robotic Sort
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 如下: #include<cstdio> #include<cstdlib&g ...
- CDN技术原理
要了解CDN的实现原理,首先让我们来回顾一下网站传统的访问过程,以便理解其与CDN访问方式之间的差别: 由上图可见,传统的网站访问过程为: 1. 用户在浏览器中输入要访问的域名: 2. 浏览器向域名解 ...
- IOS中 如何去除Tabview里面cell之间的下划线
可以利用Tabview的separatorStyle属性来设置,选择其中的UITableViewCellSeparatorStyleNone 即可去除cell之间的下划线 self.tableView ...
- ios8中的UIScreen
let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation pri ...
- rm -rf删除过多文件提示参数过长
cd /var/tmp/ find . -name "*.log"|xargs rm -rf "*.log"
- 推荐:一个个人开发者搞app赚钱之后的总结!有图有真相。
2011年已经过去了,回顾2011有收获,更有许多不足.收获就是了却了一件人生大事(女儿出生),还有就是算入门了android并利用它开发 了一 款还算有些许收获的应用.不足的地方是单位工作上没有太好 ...
- android NDK开发环境搭建
android NDK开发环境搭建 2012-05-14 00:13:58 分类: 嵌入式 基于 Android NDK 的学习之旅-----环境搭建 工欲善其事必先利其器 , 下面介绍下 Eclip ...