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 ...
随机推荐
- phonegap 新窗口 inappbrowser插件
在Phonegap 开发过程中,需要调用外部网页,又要跳出白名单安全限制,可以使用 inappbrowser插件. http://plugins.cordova.io/#/package/org.ap ...
- javascript面向对象事件继承
继承:父类有的,子类也有.父类改变,子类也跟着变. 属性继承: 矫正this (window对象,矫正成object对象) fn .call(this是谁,参数1,参数2...); ...
- delphi请求idhttp数据
idhttp ss : TStringStream; begin ss := TStringStream.)); { 指定gb2312的中文代码页,或者54936(gb18030)更好些 utf8 对 ...
- VS2010在C#头文件添加文件注释的方法(转)
步骤: 1.VS2010 中找到(安装盘符以C盘为例)C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCa ...
- WEB数据挖掘(十六)——Aperture数据抽取(9):数据源
One of the central concepts of Aperture is the notion of a DataSource. A DataSource contains all inf ...
- Struts2 高危漏洞补丁版本为: Struts 2.3.15.1
Struts2 昨天爆出高危漏洞,黑客利用这个漏洞可以执行任意命令(包括恶意的jsp代码),轻松绕过您的验证系统,登陆您的网站后台,使您的网站后台密码形同虚设!! 目前Struts2官方已经发布了一个 ...
- Linux下安装Android Studio (Centos 7)
首先去下载一个android studio的包. http://www.android-studio.org/ (友情一个) http://www.cnblogs.com/gssl/p/4963668 ...
- Linux 禁用笔记本触摸板
1. 查看有什么设备 xinput list 输出: ⎡ Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core XTEST p ...
- OC中控制台日志打印
OC中Debug版本常用的打印格式化操作 %@ 对象 %d,%i 整型 (%i的老写法) %hd 短整型 %ld , %lld 长整型 %u 无符整型 %f 浮点型和doubl ...
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.3 Details用户详细信息]
3.3 Details用户详细信息 用户详细信息是通过objectId获取.代码如下 public async Task<ActionResult> Details(string obje ...