当前位置:网站首页>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-106: construct a binary tree according to the sequence of middle and later traversal
- 重写波士顿房价预测任务(使用飞桨paddlepaddle)
- Handwritten digit recognition: CNN alexnet
- 神经网络入门之模型选择(PyTorch)
- Ind yff first week
- Julia1.0
- 深度学习入门之线性回归(PyTorch)
- Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
- Leetcode刷题---1385
- GAOFAN Weibo app
猜你喜欢

Configure opencv in QT Creator

Policy Gradient Methods of Deep Reinforcement Learning (Part Two)

Ut2014 learning notes

Boston house price forecast (tensorflow2.9 practice)

ThreadLocal原理及使用场景

I really want to be a girl. The first step of programming is to wear women's clothes

Model evaluation and selection

权重衰退(PyTorch)
![[C question set] of Ⅵ](/img/49/eb31cd26f7efbc4d57f17dc1321092.jpg)
[C question set] of Ⅵ

Class-Variant Margin Normalized Softmax Loss for Deep Face Recognition
随机推荐
Secure in mysql8.0 under Windows_ file_ Priv is null solution
Ind FXL first week
Policy gradient Method of Deep Reinforcement learning (Part One)
【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
2-program logic
Boston house price forecast (tensorflow2.9 practice)
熵值法求权重
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
Leetcode刷题---852
High imitation bosom friend manke comic app
Leetcode-404: sum of left leaves
Adaptive Propagation Graph Convolutional Network
多层感知机(PyTorch)
八、MySQL之事务控制语言
A super cool background permission management system
High imitation Netease cloud music
Leetcode刷题---832
Label Semantic Aware Pre-training for Few-shot Text Classification
Leetcode刷题---704
I really want to be a girl. The first step of programming is to wear women's clothes