LeetCode44 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
分析:
跟第10题Regular Expression Matching很像,从正则表达式匹配变成了通配符匹配,用动态规划的方法做的话, 比之前这个题要来的简单。
还是双序列动态规划,用dp[i][j]表示s前i个与p前j个是否能匹配。
分p[j - 1]的几种情况,
当 (p[j - 1] == s[i - i] 或者 p[j - 1] == '?')且 dp[i - 1][j - 1] == true, 则 dp[i][j] == true;
当 p[j - 1] == '*'时, (dp[i - 1][j] == true|| dp[i][j - 1] == true), 则 dp[i][j] == true;
初始化第一行,第一列即可。
注意: 动归的算法在leetcode上有两组样例应该是过不了的,可能还有贪心的思路可以优化,但动归的思路应该更值得学习(更有通用性),回头有时间再来补上贪心的思路。
如果要通过样例的话,可以有个小作弊,就是当s.size() > 3000时,返回false,处理掉那两个大样例。
代码:
class Solution {
public:
bool isMatch(string s, string p) {
if (p.size() > || s.size() > ) {
return false;
}
bool dp[s.size() + ][p.size() + ] = {false};
dp[][] = true;
for (int i = ; i <= p.size(); ++i) {
dp[][i] = dp[][i - ] && (p[i - ] == '*');
}
for (int i = ; i <= s.size(); ++i) {
for (int j = ; j <= p.size(); ++j) {
if ((p[j - ] == s[i - ] || p[j - ] == '?') && dp[i - ][j - ] == true) {
dp[i][j] = true;
}
if (p[j - ] == '*' && (dp[i - ][j] || dp[i][j - ]) ){
dp[i][j] = true;
}
}
}
return dp[s.size()][p.size()];
}
};
LeetCode44 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 '*'. '.' ...
- leetcode44:wildcard
44. Wildcard Matching 问题描述 给定字符串s和模式p,判断字符串s是否完全符合模式p 其中字符串s只包含小写字母,模式串p包含小写字母.*.?,其中星号表示任意长度的任意字符串, ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
随机推荐
- 第三百四十七天 how can I 坚持
下班的时候眼皮就一直在跳,今天意志好消沉,以后还是少说话,多说不宜啊.. 挣脱束缚,无论怎样,对于生命,什么都是次要的,不要想太多. 最近事比较多,应该是累了,睡一觉 应该就好了. 睡觉,晚安.
- 常见mongo命令
@(编程) 查询 db.getCollection('SalaryEntity').find({"Month" : "201601"}) db.getColle ...
- EntityFramework简单例子
@(编程) 这个例子是用vs2013连接mysql数据库. 1. NuGet安装EF和mysql 略 2. 对象 namespace EFDemo { class Student { public s ...
- OracleCommand.CommandText 无效
OracleCommand insertADataCmd = conn.CreateCommand(); insertBDataCmd.CommandText = SQL OracleParamete ...
- 埃氏筛法(快速筛选n以内素数的个数)
给你一个数n,请问n以内有多少个素数?(n <= 10e7) 一般来说,要是对一个整数进行素数判断,首先想到的是写个函数判断是否为素数,然后调用这个函数,时间复杂度为O(n^(½)),但是要求n ...
- C#中DllImport用法和路径问题
DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息. DllImport属性应用于方法,要 ...
- mysql 字段操作
1.添加字段 ALTER TABLE lucky_user ADD COLUMN id_type TINYINT NOT NULL DEFAULT '0' COMMENT "0: 普通用户, ...
- android 工具类之SharePreference
/** * SharedPreferences的一个工具类,调用setParam就能保存String, Integer, Boolean, Float, Long类型的参数 * 同样调用getPara ...
- SCROLLINFO结构体中fMask和nPage的理解
还是VC++中有关显示图像的问题. 我们在显示一幅比较大的图像时,要使用带标准滚动条的对话框.涉及对滚动条的操作就不得不提SCROLLINFO这个结构体.只看单词意思就这道这个结构体要储存滚动条的一些 ...
- Flex坐标
flash和flex针对不同的目的,提供了3种不同的坐标系. 全局的就是(stage级别的) 本地坐标系(组件级别的) 内容坐标系(相对于本地坐标系说的) 这些坐标系的点是可以转换的,并且有相应的方法 ...