当前位置:网站首页>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
- Handwritten digit recognition: CNN alexnet
- I really want to be a girl. The first step of programming is to wear women's clothes
- 权重衰退(PyTorch)
- Flutter 退出当前操作二次确认怎么做才更优雅?
- SQL Server Management Studio cannot be opened
- Simple real-time gesture recognition based on OpenCV (including code)
- Deep Reinforcement learning with PyTorch
- MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
- Leetcode刷题---1385
猜你喜欢

High imitation Netease cloud music

Ind kwf first week

C#项目-寝室管理系统(1)

A super cool background permission management system

『快速入门electron』之实现窗口拖拽

An open source OA office automation system

深度学习入门之自动求导(Pytorch)

Hands on deep learning pytorch version exercise solution - 2.3 linear algebra

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

重写波士顿房价预测任务(使用飞桨paddlepaddle)
随机推荐
High imitation bosom friend manke comic app
八、MySQL之事务控制语言
Leetcode - the k-th element in 703 data flow (design priority queue)
Ut2014 supplementary learning notes
神经网络入门之模型选择(PyTorch)
Handwritten digit recognition: CNN alexnet
20220610 other: Task Scheduler
Tensorflow - tensorflow Foundation
Ut2017 learning notes
What did I read in order to understand the to do list
Leetcode刷题---367
Leetcode刷题---1385
20220531 Mathematics: Happy numbers
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
[LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression
Leetcode刷题---189
Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
Ind yff first week
Leetcode刷题---10