当前位置:网站首页>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)
边栏推荐
- Matrix calculation of Neural Network Introduction (pytoch)
- What can I do to exit the current operation and confirm it twice?
- Adaptive Propagation Graph Convolutional Network
- Handwritten digit recognition: CNN alexnet
- 侯捷——STL源码剖析 笔记
- 深度学习入门之线性回归(PyTorch)
- Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation
- What useful materials have I learned from when installing QT
- Leetcode刷题---852
- Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
猜你喜欢
Hands on deep learning pytorch version exercise solution - 3.1 linear regression
Ind wks first week
Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
Matrix calculation of Neural Network Introduction (pytoch)
High imitation wechat
深度学习入门之自动求导(Pytorch)
Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
Knowledge map enhancement recommendation based on joint non sampling learning
A complete answer sheet recognition system
随机推荐
mysql5.7安装和配置教程(图文超详细版)
What did I read in order to understand the to do list
一个30岁的测试员无比挣扎的故事,连躺平都是奢望
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
Leetcode-513: find the lower left corner value of the tree
Model evaluation and selection
『快速入门electron』之实现窗口拖拽
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
Matrix calculation of Neural Network Introduction (pytoch)
Automatic derivation of introduction to deep learning (pytoch)
Class-Variant Margin Normalized Softmax Loss for Deep Face Recognition
20220607 others: sum of two integers
Leetcode-100: same tree
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
Leetcode刷题---977
Policy gradient Method of Deep Reinforcement learning (Part One)
Knowledge map enhancement recommendation based on joint non sampling learning
[LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression
Leetcode刷题---832
LeetCode - 715. Range module (TreeSet)*****