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 ...
随机推荐
- es6转码器-babel
babel 基本使用 安装转码规则 # ES2015转码规则 $ npm install --save-dev babel-preset-es2015 # react转码规则 $ npm instal ...
- html5 canvas防微博旋转
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 第三百五十七天 how can I 坚持
502是我对你没有爱的意思吗?为什么要要这样啊,好绝情. 明天要去加班,今晚回来也好晚了,晚上回来都有点精神恍惚了. 他们要聚会,本来要想去樱木花道来,哎. 后天..什么都没学. .. .. .. 准 ...
- GPIO 配置之ODR, BSRR, BRR 详解
STM32 GPIO 配置之ODR, BSRR, BRR 详解 用stm32 的配置GPIO 来控制LED 显示状态,可用ODR,BSRR,BRR 直接来控制引脚输出状态. ODR寄存器可读可写:既能 ...
- 咏南WEB开发框架
和咏南CS开发框架共享同一个咏南中间件.
- ASP.NET前台AJAX方法调用后台的方法写法
前台: <input id="AjaxDemo" type="button" onclick="get()" value=" ...
- POJ3345
http://poj.org/problem?id=3345 大意: 大意是说现在有n个城市来给你投票,你需要至少拿到m个城市的赞成票.想要获得第i个城市的赞成需要花费w[i],有个条件就是某些城市是 ...
- UVa11235 FrequentValues(RMQ)
Problem F: Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasi ...
- UVa 11536 Smallest Sub-Array (水题, 滑动窗口)
题意:给定 n 个由0~m-1的整数组成的序列,输入 k ,问你找出连续的最短序列,使得这个序列含有1-k的所有整数. 析:这个题,很简单么,只要从头开始扫一遍就OK,时间复杂度为O(n). 代码如下 ...
- 利用花生壳和IIS发布网页过程
老早利用做过类似的事情,但最近又忘了怎么弄的了,还是自己给自己总结下,省得以后到处找了. [动态域名绑定] 如果具有公网IP地址,申请一个免费的花生壳动态域名,再下一个花生壳客户,使用已经申请好的动态 ...