当前位置:网站首页>Leetcode 44 Wildcard matching (2022.02.13)
Leetcode 44 Wildcard matching (2022.02.13)
2022-07-06 00:33:00 【ChaoYue_ miku】
Given a string (s) And a character pattern § , 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 = “ab”
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.com/problems/wildcard-matching
Method 1 : Dynamic programming
C++ Submission :
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size();
int n = p.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
dp[0][0] = true;
for (int i = 1; i <= n; ++i) {
if (p[i - 1] == '*') {
dp[0][i] = true;
}else {
break;
}
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (p[j - 1] == '*') {
dp[i][j] = dp[i][j - 1] | dp[i - 1][j];
}else if (p[j - 1] == '?' || s[i - 1] == p[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
};
边栏推荐
- FFmpeg抓取RTSP图像进行图像分析
- Spark AQE
- NLP generation model 2017: Why are those in transformer
- Reading notes of the beauty of programming
- MySql——CRUD
- MySQL之函数
- DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
- Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
- 数据分析思维分析方法和业务知识——分析方法(三)
- AtCoder Beginner Contest 258【比赛记录】
猜你喜欢
数据分析思维分析方法和业务知识——分析方法(三)
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
notepad++正则表达式替换字符串
Browser local storage
【DesignMode】组合模式(composite mode)
Classical concurrency problem: the dining problem of philosophers
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
MySql——CRUD
uniapp开发,打包成H5部署到服务器
随机推荐
Spark AQE
数据分析思维分析方法和业务知识——分析方法(三)
AtCoder Beginner Contest 258【比赛记录】
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
The relationship between FPGA internal hardware structure and code
Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
Spark DF增加一列
如何制作自己的機器人
Uniapp development, packaged as H5 and deployed to the server
NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
关于slmgr命令的那些事
MySQL存储引擎
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
数据分析思维分析方法和业务知识——分析方法(二)
Spark DF adds a column
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
Ffmpeg learning - core module
Browser reflow and redraw
【DesignMode】装饰者模式(Decorator pattern)