当前位置:网站首页>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)
边栏推荐
- Secure in mysql8.0 under Windows_ file_ Priv is null solution
- Data classification: support vector machine
- Matrix calculation of Neural Network Introduction (pytoch)
- I really want to be a girl. The first step of programming is to wear women's clothes
- 神经网络入门之矩阵计算(Pytorch)
- Leetcode刷题---832
- 【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
- Label Semantic Aware Pre-training for Few-shot Text Classification
- 20220608 other: evaluation of inverse Polish expression
- Flutter 退出当前操作二次确认怎么做才更优雅?
猜你喜欢

Matrix calculation of Neural Network Introduction (pytoch)

Policy Gradient Methods of Deep Reinforcement Learning (Part Two)

Advantageous distinctive domain adaptation reading notes (detailed)

Linear regression of introduction to deep learning (pytorch)

Leetcode - 1172 plate stack (Design - list + small top pile + stack))

A super cool background permission management system

Knowledge map reasoning -- hybrid neural network and distributed representation reasoning

神经网络入门之矩阵计算(Pytorch)

Raspberry pie 4B deploys lnmp+tor and builds a website on dark web

Tensorflow - tensorflow Foundation
随机推荐
Label Semantic Aware Pre-training for Few-shot Text Classification
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
A complete answer sheet recognition system
Ut2012 learning notes
Pytorch ADDA code learning notes
Ind FHL first week
A complete mall system
重写波士顿房价预测任务(使用飞桨paddlepaddle)
Policy gradient Method of Deep Reinforcement learning (Part One)
熵值法求权重
Tensorflow—Image segmentation
Softmax 回归(PyTorch)
Leetcode刷题---278
Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
神经网络入门之预备知识(PyTorch)
Leetcode刷题---852
20220606 Mathematics: fraction to decimal
Training effects of different data sets (yolov5)
Leetcode-513: find the lower left corner value of the tree