当前位置:网站首页>LeetCode_动态规划_困难_44.通配符匹配
LeetCode_动态规划_困难_44.通配符匹配
2022-07-21 23:04:00 【小城老街】
1.题目
给定一个字符串 ( s ) 和一个字符模式 ( p ) ,实现一个支持 ‘?’ 和 ‘*’ 的通配符匹配。
‘?’ 可以匹配任何单个字符。
‘*’ 可以匹配任意字符串(包括空字符串)。
两个字符串完全匹配才算匹配成功。
说明:
s 可能为空,且只包含从 a-z 的小写字母。
p 可能为空,且只包含从 a-z 的小写字母,以及字符 ? 和 *。
示例 1:
输入:
s = "aa"
p = "a"
输出: false
解释: "a" 无法匹配 "aa" 整个字符串。
示例 2:
输入:
s = "aa"
p = "*"
输出: true
解释: '*' 可以匹配任意字符串。
示例 3:
输入:
s = "cb"
p = "?a"
输出: false
解释: '?' 可以匹配 'c', 但第二个 'a' 无法匹配 'b'。
示例 4:
输入:
s = "adceb"
p = "*a*b"
输出: true
解释: 第一个 '*' 可以匹配空字符串, 第二个 '*' 可以匹配字符串 "dce".
示例 5:
输入:
s = "acdcb"
p = "a*c?b"
输出: false
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/wildcard-matching
2.思路
(1)动态规划
思路参考本题官方题解。
3.代码实现(Java)
//思路1————动态规划
class Solution {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
//dp[i][j] 表示字符串 s 的前 i 个字符和模式 p 的前 j 个字符是否能匹配
boolean[][] dp = new boolean[m + 1][n + 1];
dp[0][0] = true;
for (int i = 1; i <= n; i++) {
if (p.charAt(i - 1) == '*') {
dp[0][i] = true;
} else {
break;
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p.charAt(j - 1) == '*') {
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
} else if (p.charAt(j - 1) == '?' || s.charAt(i - 1) == p.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
}
边栏推荐
- Use of watch in projects
- JVM的运行原理
- uniapp常用的生命周期
- The difference between EM and REM
- 2022/07/19 --- stack push in and pop-up sequence; String representing numeric value
- How to use call, bind and apply correctly
- PMP每日一练 | 考试不迷路-7.21
- 自动化设备制造行业常见管理难题及解决方案
- (iccv-2021) transfeid: Target Recognition Based on transformer
- Esp8266 analog input (ADC) detection problem
猜你喜欢
How does am5se-is anti islanding protection device solve the impact in the process of distributed photovoltaic power generation?
ClickHouse引擎之-MaterializeMYSQL
边框动态效果实现
PMP practice once a day | don't get lost in the exam -7.21
How does Siemens PLC in the factory control room collect the production data of multiple production lines in a centralized and wireless way?
MYSQL8存储过程生成日历表以及异常处理
ClickHouse的安装
[ kitex 源码解读 ] Kitex 扩展性设计思路
The difference between join on followed by condition and where followed by condition
The use of splice method in JS
随机推荐
Qt Creator快捷键大全,附快捷键配置方法
Addition, deletion, query and modification of [mdsql] table (Advanced)
Application of ERP system in component trading enterprises
Typescript - syntax introduction
边框动态效果实现
超干货!彻底搞懂单工、半双工、全双工的区别与联系
One bite of Stream(8)
Messy analysis view system
Elemen when clicking, modify the playback index of the walking lantern
JVM-双亲委派机制
How to use call, bind and apply correctly
PMP每日一练 | 考试不迷路-7.21
Procurement control: shorten the procurement cycle, reduce costs and increase efficiency from the source
2022/07/21 --- maximum value of sliding window;
JVM(9)之JVM对象创建与内存分配深度剖析
深入理解完美哈希
同一个浏览器不同窗口登录不同账号,窗口切换时,页面刷新账号变更为最后一次登录的账号
会员营销怎么做? 3个留住顾客的小秘诀!
(iccv-2021) transfeid: Target Recognition Based on transformer
Qt中的坐标系统