题目
Given an input string (s) and a pattern §, implement wildcard pattern matching with support for ‘?’ and ‘*’.
‘?’ Matches any single character.
‘*’ Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
Note:
s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like ? or *.
Example 1:
Input:
s = “aa”
p = “a”
Output: false
Explanation: “a” does not match the entire string “aa”.
Example 2:
Input:
s = “aa”
p = ""
Output: true
Explanation: '’ matches any sequence.
Example 3:
Input:
s = “cb”
p = “?a”
Output: false
Explanation: ‘?’ matches ‘c’, but the second letter is ‘a’, which does not match ‘b’.
Example 4:
Input:
s = “adceb”
p = “ab”
Output: true
Explanation: The first ‘’ matches the empty sequence, while the second '’ matches the substring “dce”.
Example 5:
Input:
s = “acdcb”
p = “a*c?b”
Output: false
NFA方法
思路
这里我们采用NFA(Nodeterministic Finite Automation,非确定有限状态机)的思想,用p构建一个NFA,用s作为输入,在整个迭代的过程中维护一个available
的数组,这个数组表示NFA所处的状态。available
中的元素是指向p中字符的迭代器,表示当前希望接受到的元素。当p.end()
出现在available
中时,说明NFA不会再希望接收到任何元素,即字符串实现成功匹配。
代码中需要注意到的一个小细节是,在每次迭代获得新的available
时,需要对available
内的元素求一次
ε
−
\varepsilon-
ε−闭包(概念见《编译原理》,龙书),即求出当前状态下不接受任何输入可以到达的状态,并把这些新的状态加入到available
中。
代码
class Solution {
public:
bool isMatch(string s, string p) {
set<string::iterator> available;
{
auto _avail = p.begin();
while(_avail != p.end() && *_avail == '*') {
available.insert(_avail);
++_avail;
}
available.insert(_avail);
}
for(char ch : s) {
set<string::iterator> new_a;
for(auto avail : available) {
if(avail == p.end()) {
continue;
}
if(*avail == '*') {
new_a.insert(avail + 1);
new_a.insert(avail);
} else if(*avail == '?' || *avail == ch) {
new_a.insert(avail + 1);
}
}
available.clear();
for(auto avail : new_a) {
auto _avail = avail;
while(_avail != p.end() && *_avail == '*') {
available.insert(_avail);
++_avail;
}
available.insert(_avail);
}
}
return available.find(p.end()) != available.end();
}
};
DFS方法
NFA方法虽然通用,但在这个问题里面,我们可以采用更高效的DFS搜索(代码出处)
class Solution {
// return value:
// 0: reach the end of s but unmatched
// 1: unmatched without reaching the end of s
// 2: matched
int dfs(string& s, string& p, int si, int pi) {
if(si == s.size() && pi == p.size()) return 2;
if(si == s.size() && p[pi] != '*') return 0;
if(pi == p.size()) return 1;
if(p[pi] == '*') {
if(pi + 1 < p.size() && p[pi + 1] == '*')
return dfs(s, p, si, pi + 1); // skip duplicate '*'
for(int i = 0; i <= s.size() - si; ++i) {
int ret = dfs(s, p, si + i, pi + 1);
if(ret == 0 || ret == 2) return ret;
}
}
if(p[pi] == '?' || s[si] == p[pi])
return dfs(s, p, si + 1, pi + 1);
return 1;
}
public:
bool isMatch(string s, string p) {
return dfs(s, p, 0, 0) > 1;
}
};