当前位置:网站首页>LeetCode_ Dynamic programming_ Difficulties_ 44. Wildcard matching
LeetCode_ Dynamic programming_ Difficulties_ 44. Wildcard matching
2022-07-22 13:32:00 【Old street of small town】
1. subject
Given a string ( s ) And a character pattern ( p ) , Implement a support ‘?’ and ‘*’ The wildcard matches .
‘?’ It can match any single character .
‘*’ Can match any string ( Include empty string ).
Only when two strings match exactly can the match be successful .
explain :
s May is empty , And contains only from a-z Lowercase letters of .
p May is empty , And contains only from a-z Lowercase letters of , As well as the character ? and *.
Example 1:
Input :
s = "aa"
p = "a"
Output : false
explain : "a" Can't match "aa" Whole string .
Example 2:
Input :
s = "aa"
p = "*"
Output : true
explain : '*' Can match any string .
Example 3:
Input :
s = "cb"
p = "?a"
Output : false
explain : '?' Can match 'c', But the second one 'a' Can't match 'b'.
Example 4:
Input :
s = "adceb"
p = "*a*b"
Output : true
explain : first '*' Can match an empty string , the second '*' Can match string "dce".
Example 5:
Input :
s = "acdcb"
p = "a*c?b"
Output : false
source : Power button (LeetCode)
link :https://leetcode.cn/problems/wildcard-matching
2. Ideas
(1) Dynamic programming
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— Dynamic programming
class Solution {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
//dp[i][j] Representation string s Before i Characters and patterns p Before j Can characters match
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];
}
}
边栏推荐
- 简单易用的任务队列-beanstalkd
- Addition, deletion, query and modification of [mysql] table (basic)
- ApacheCon Asia 2022 开启报名:Pulsar 技术议题重磅亮相
- 乱七八糟的分析View体系
- Supply chain collaborative management system of pharmaceutical machinery industry: full link digital coverage to realize the visualization of industrial supply chain
- SSM之简单的CRUD
- MySQL basic functions
- Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
- 2022/07/20--- convert string to integer; Maximum value of sliding window
- RecordRTC的视频录制,回放,截图,下载
猜你喜欢
ERP系统在元器件贸易企业中的应用
mySQL基本函数
Digital supply chain management system for intelligent instrument industry: accelerating the transformation of enterprise intelligent supply chain platform
边框动态效果实现
617. Merge binary tree
Digital business cloud: under the trend of multiple supplier scenarios, how can garment enterprises build a flexible SRM management system?
如何安装mysql
Application of ERP system in component trading enterprises
利用浏览器插件运行js来删除特定网站“禁用copy”功能
Mysql8 stored procedure generates calendar table and exception handling
随机推荐
Mysql8 stored procedure generates calendar table and exception handling
224. 基本计算器-递归法
leetcode-720:词典中最长的单词
service(lb) 和管理的pod
Operating principle of JVM
win10如何设置锁屏后不熄屏
[MySQL]数据库基础操作
Qt开发应用程序Debug与Release设置
[advanced C language] learning about flexible arrays
稀土开发者大会|StreamNative 翟佳、刘德志分享云原生技术变革之路
Pyhton crawls the primary and secondary website pages and saves the crawled image information locally
SSM之简单的CRUD
[PostgreSQL 15] PostgreSQL 15 improves unique and null
The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
2022/07/20--- convert string to integer; Maximum value of sliding window
One bite of Stream(9)
【等保小知识】等保整改是什么意思?整改内容包括哪些?
MySQL basic functions
如何将沥青高位槽液位数值无线传输至载热体记录仪监测?
[有趣] VS Code -- Live Server的一小段代码注入