Leetcode Regular Expression matching

Source: Internet
Author: User
class Solution {#define SINGLE 1#define MULTIP 2public:    bool isMatch(const char *s, const char *p) {        if (s == NULL || p == NULL) return true;                vector<pair<char, int> > pattern;                int     pos = 0;        char     ch = ‘\0‘;                while ((ch = p[pos++]) != ‘\0‘) {            if (ch == ‘*‘) {                pattern.back().second = MULTIP;                continue;            }            pattern.push_back(make_pair(ch, SINGLE));        }                return isMatch(s, 0, pattern, 0);    }        bool isMatch(const char *s, int pos, vector<pair<char, int> > &pattern, int ppos) {        if (ppos == pattern.size()) {            return s[pos] == ‘\0‘;        }        int i = pos;        pair<char, int> &p = pattern[ppos];        int     offset = (p.second == SINGLE) ? 0 : -1;        char     ch = ‘\0‘;        while (offset < 0 || (ch = s[pos + offset]) != ‘\0‘) {            if (offset >= 0 && !cmp(ch, p.first)) {                return false;            }            if (isMatch(s, pos + offset + 1, pattern, ppos + 1)) {                return true;            }            if (p.second == SINGLE) break;            offset++;        }        return false;    }        bool cmp(const char a, const char b) {        if (a == ‘.‘ || b == ‘.‘) return true;        return a == b;    }};

Violent law, 216 ms,

 

 

Rewrite as a memory search. Here we assume that the length of the string and the mode string cannot exceed the unsigned short range (6 W +), 84 Ms

class Solution {#define SINGLE 1#define MULTIP 2private:    unordered_map<unsigned int, bool> memo;public:    bool isMatch(const char *s, const char *p) {        if (s == NULL || p == NULL) return true;        memo.clear();                vector<pair<char, int> > pattern;                int     pos = 0;        char     ch = ‘\0‘;                while ((ch = p[pos++]) != ‘\0‘) {            if (ch == ‘*‘) {                pattern.back().second = MULTIP;                continue;            }            pattern.push_back(make_pair(ch, SINGLE));        }                return isMatch(s, 0, pattern, 0);    }        bool isMatch(const char *s, int pos, vector<pair<char, int> > &pattern, int ppos) {        unsigned int id = (pos << 16) | ppos;                unordered_map<unsigned int, bool>::iterator iter = memo.find(id);        if (memo.find(id) != memo.end()) return iter->second;                bool res = false;                if (ppos == pattern.size()) {            res = s[pos] == ‘\0‘;            memo.insert(make_pair(id, res));            return res;        }        int i = pos;        pair<char, int> &p = pattern[ppos];        int     offset = (p.second == SINGLE) ? 0 : -1;        char     ch = ‘\0‘;        while (offset < 0 || (ch = s[pos + offset]) != ‘\0‘) {            if (offset >= 0 && !cmp(ch, p.first)) {                res = false;                break;            }            if (isMatch(s, pos + offset + 1, pattern, ppos + 1)) {                res = true;                break;            }            if (p.second == SINGLE) break;            offset++;        }        memo.insert(make_pair(id, res));        return res;    }        bool cmp(const char a, const char b) {        if (a == ‘.‘ || b == ‘.‘) return true;        return a == b;    }};

 

Leetcode Regular Expression matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.