[leetcode] Wildcard Matching

题目

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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值