当前位置:网站首页>Leetcode skimming ---44
Leetcode skimming ---44
2022-07-03 10:35:00 【Long time no see 0327】
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 *.
Input :s = "aa" ,p = "a"
Output :false
Method 1 : Dynamic programming
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];
}
};
Complexity analysis
Time complexity :O(mn)
Spatial complexity :O(mn)
Method 2 : Greedy Algorithm
class Solution {
public:
bool isMatch(string s, string p) {
auto allStars = [](const string& str, int left, int right) {
for (int i = left; i < right; ++i) {
if (str[i] != '*') {
return false;
}
}
return true;
};
auto charMatch = [](char u, char v) {
return u == v || v == '?';
};
while (s.size() && p.size() && p.back() != '*') {
if (charMatch(s.back(), p.back())) {
s.pop_back();
p.pop_back();
} else {
return false;
}
}
if (p.empty()) {
return s.empty();
}
int sIndex = 0, pIndex = 0;
int sRecord = -1, pRecord = -1;
while (sIndex < s.size() && pIndex < p.size()) {
if (p[pIndex] == '*') {
++pIndex;
sRecord = sIndex;
pRecord = pIndex;
} else if (charMatch(s[sIndex], p[pIndex])) {
++sIndex;
++pIndex;
} else if (sRecord != -1 && sRecord + 1 < s.size()) {
++sRecord;
sIndex = sRecord;
pIndex = pRecord;
} else {
return false;
}
}
return allStars(p, pIndex, p.size());
}
};
Complexity analysis
Time complexity :O(mn)
Spatial complexity :O(mlogn)
边栏推荐
- Leetcode - 5 longest palindrome substring
- [LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
- EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
- Ut2015 learning notes
- Leetcode刷题---44
- OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
- Softmax 回归(PyTorch)
- Ind FHL first week
- Stroke prediction: Bayesian
- Leetcode-404:左叶子之和
猜你喜欢
Flutter 退出当前操作二次确认怎么做才更优雅?
Jetson TX2 刷机
Handwritten digit recognition: CNN alexnet
ECMAScript -- "ES6 syntax specification # Day1
Advantageous distinctive domain adaptation reading notes (detailed)
Simple real-time gesture recognition based on OpenCV (including code)
Hands on deep learning pytorch version exercise solution -- implementation of 3-2 linear regression from scratch
Adaptive Propagation Graph Convolutional Network
Ut2017 learning notes
High imitation bosom friend manke comic app
随机推荐
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
2-program logic
Seata分布式事务失效,不生效(事务不回滚)的常见场景
神经网络入门之模型选择(PyTorch)
Ind FXL first week
20220607其他:两整数之和
Inverse code of string (Jilin University postgraduate entrance examination question)
Leetcode刷题---189
Hands on deep learning pytorch version exercise solution -- implementation of 3-2 linear regression from scratch
神经网络入门之预备知识(PyTorch)
【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
Raspberry pie 4B installs yolov5 to achieve real-time target detection
Data classification: support vector machine
Ut2011 learning notes
Automatic derivation of introduction to deep learning (pytoch)
Leetcode-112: path sum
Ut2014 supplementary learning notes
重写波士顿房价预测任务(使用飞桨paddlepaddle)
二分查找法