[Leetcode 44]通配符匹配Wildcard Matching
【题目】
匹配通配符*,?,DP动态规划,重点是*的两种情况
想象成两个S、P长度的字符串,P匹配S。
S中不会出现通配符。
【条件】
(1)P=null,S=null,TRUE
(2)P=null,S!=null,P必然无法匹配S,FALSE。
(3)P[i]=“*” 的TRUE/FALSE状态等价于P[i-1]
(4)考虑*两种情况,ab, ab*(*=null)、abcd, ab*(*=cd)
【参考】
The most confusing part for me is how to deal with '*'. At first I couldn't figure out why the condition would be (dp[i-1][j] == true || dp[i][j-1] == true). Hope detailed DP description below helps!
- dp[i][j]: true if the first i char in String s matches the first j chars in String p
- Base case:
- origin: dp[0][0]: they do match, so dp[0][0] = true
- first row: dp[0][j]: except for String p starts with *, otherwise all false
- first col: dp[i][0]: can't match when p is empty. All false.
- Recursion:
- Iterate through every dp[i][j]
- dp[i][j] = true:
- if (s[ith] == p[jth] || p[jth] == '?') && dp[i-1][j-1] == true
- elif p[jth] == '*' && (dp[i-1][j] == true || dp[i][j-1] == true)
-for dp[i-1][j], means that * acts like an empty sequence.
eg: ab, ab*
-for dp[i][j-1], means that * acts like any sequences
eg: abcd, ab*
- Start from 0 to len
- Output put should be dp[s.len][p.len], referring to the whole s matches the whole p
Be careful about the difference of index i,j in String (0 to len-1) and the index i, j in dp (0 to len)!
Below is my AC code in Java:
public boolean isMatch(String s, String p) {
if(s == null || p == null) return false;
int sLen = s.length();
int pLen = p.length();
boolean[][] dp = new boolean[sLen + 1][pLen + 1];
// Base cases:
dp[0][0] = true;
for(int i = 1; i <= sLen; i++){
dp[i][0] = false;
}
for(int j = 1; j <= pLen; j++){
if(p.charAt(j-1) == '*'){
dp[0][j] = dp[0][j-1];
}
}
// Recursion:
for(int i = 1; i <= sLen; i++){
for(int j = 1; j <= pLen; j++){
if((s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1) == '?') && dp[i-1][j-1])
dp[i][j] = true;
else if (p.charAt(j-1) == '*' && (dp[i-1][j] || dp[i][j-1]))
dp[i][j] = true;
}
}
return dp[sLen][pLen];
}
[Leetcode 44]通配符匹配Wildcard Matching的更多相关文章
- Java实现 LeetCode 44 通配符匹配
44. 通配符匹配 给定一个字符串 (s) 和一个字符模式 § ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字 ...
- [leetcode] 44. 通配符匹配(Java)(动态规划)
44. 通配符匹配 动态规划 做动态规划很简单,三步走: 第一步,判断可否用动态规划做,即判断是否满足两个条件:①最优子结构,②重叠子问题.显然该题求s与p是否match,可由其字串层层分解上来. 我 ...
- LeetCode 44. 通配符匹配(Wildcard Matching)
题目描述 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完 ...
- [Swift]LeetCode44. 通配符匹配 | Wildcard Matching
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- leetcode 第43题 Wildcard Matching
题目:(这题好难.题目意思类似于第十题,只是这里的*就是可以匹配任意长度串,也就是第十题的‘.*’)'?' Matches any single character. '*' Matches any ...
- leetcode 44 字符匹配
题意:s是空串或包含a-z字母: p为包含a-z字母或?或 * (其中*可以匹配任意字符串包括空串,?可以匹配任意字符). 思路: 1)特殊情况:当s为空串时,p为连续 * 时,则连续 * 的位置都为 ...
- Leetcode44. 通配符匹配(动态规划)
44. 通配符匹配 动态规划 \(f_{i,j}\)为\(s\)匹配\(i\),\(t\)匹配\(j\)是否成功 贪心 相比之下这个思维性更强 考虑两个*,两个星号间的过渡,只需要过渡完到第二个星号, ...
- [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching
开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
随机推荐
- 质控工具之cutadapt的使用方法
cutadapt 参考:用cutadapt软件来对双端测序数据去除接头 fastqc可以用于检测,检测出来了怎么办? 看了几篇高水平文章,有不少再用cutadapt,虽然有时候数据真的不错,但是还是要 ...
- DNA sequence open reading frames (ORFs) | DNA序列的开放阅读框ORF预测
常见的ORF预测工具 Open Reading Frame Finder- NCBI ORF Finder - SMS OrfPredictor - YSU 基本概念 开放阅读框(英语:Open r ...
- English trip V1 - B 24. I'm Interested in... 我对...感兴趣 Teacher:Julia Key: (I/We/They) do/don't (He/She/it)does/doesn't
In this lesson you will learn to talk about people's interests. 课上内容(Lesson) interest v. 使…感兴趣(inter ...
- English Voice of <<if were a boy >>
<if i were a boy>中英文歌词: If I were a boy 如果我是个男孩 Even just for a day 就算只是一天 I' roll out of bed ...
- 11月24日 layouts and rendering in rails(部分没有看)
http://guides.rubyonrails.org/layouts_and_rendering.html 中文 This guide covers the basic layout feat ...
- sgu
dp第几朵花放第几瓶 104 数论 能不能除3:105 106(ex_gcd引入t求范围交) 107(大数乘的FFT) 开空间技巧108 棋盘黑白格消除109(组合数学) java平方根 ...
- Python实现一条基于POS算法的区块链
区块链中的共识算法 在比特币公链架构解析中,就曾提到过为了实现去中介化的设计,比特币设计了一套共识协议,并通过此协议来保证系统的稳定性和防攻击性. 并且我们知道,截止目前使用最广泛,也是最被大家接受的 ...
- 完整的Django入门指南学习笔记1
转自[https://blog.csdn.net/qq_35554125/article/details/79462885] part 1: 前沿 教程材料一共会被分为七个部分. 此教程将从安装.开发 ...
- Tanya and Password CodeForces - 508D (欧拉回路)
大意:给定n个长为3的子串, 求一个长为n+2的字符串包含所有子串. 相邻两个字符开一个节点, 建图跑欧拉回路, 若存在的话长度一定是$\le n+2$.
- python-django rest framework框架之渲染器
渲染器 看到的页面时什么样子的,返回数据. restframework中默认就是下面 这两个render类,它的内部实现原理是拿url中的后缀名 .json 和类中的format字段进行比较,如果re ...