{"id":4007,"date":"2018-09-17T08:59:41","date_gmt":"2018-09-17T15:59:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4007"},"modified":"2018-09-17T09:00:35","modified_gmt":"2018-09-17T16:00:35","slug":"leetcode-10-regular-expression-matching","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-10-regular-expression-matching\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 10. Regular Expression Matching"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an input string (<code>s<\/code>) and a pattern (<code>p<\/code>), implement regular expression matching with support for\u00a0<code>'.'<\/code>\u00a0and\u00a0<code>'*'<\/code>.<\/p>\n<pre class=\"crayon:false \">'.' Matches any single character.\r\n'*' Matches zero or more of the preceding element.\r\n<\/pre>\n<p>The matching should cover the\u00a0<strong>entire<\/strong>\u00a0input string (not partial).<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>s<\/code>\u00a0could be empty and contains only lowercase letters\u00a0<code>a-z<\/code>.<\/li>\n<li><code>p<\/code>\u00a0could be empty and contains only lowercase letters\u00a0<code>a-z<\/code>, and characters like\u00a0<code>.<\/code>\u00a0or\u00a0<code>*<\/code>.<\/li>\n<\/ul>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>\r\ns = \"aa\"\r\np = \"a\"\r\n<strong>Output:<\/strong> false\r\n<strong>Explanation:<\/strong> \"a\" does not match the entire string \"aa\".\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>\r\ns = \"aa\"\r\np = \"a*\"\r\n<strong>Output:<\/strong> true\r\n<strong>Explanation:<\/strong>\u00a0'*' means zero or more of the precedeng\u00a0element, 'a'. Therefore, by repeating 'a' once, it becomes \"aa\".\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>\r\ns = \"ab\"\r\np = \".*\"\r\n<strong>Output:<\/strong> true\r\n<strong>Explanation:<\/strong>\u00a0\".*\" means \"zero or more (*) of any character (.)\".\r\n<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>\r\ns = \"aab\"\r\np = \"c*a*b\"\r\n<strong>Output:<\/strong> true\r\n<strong>Explanation:<\/strong>\u00a0c can be repeated 0 times, a can be repeated 1 time. Therefore it matches \"aab\".\r\n<\/pre>\n<p><strong>Example 5:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>\r\ns = \"mississippi\"\r\np = \"mis*is*p*.\"\r\n<strong>Output:<\/strong> false\r\n<\/pre>\n<h1><strong>Solution 1: Recursion<\/strong><\/h1>\n<p>Time complexity: O((|s| + |p|) * 2 ^\u00a0(|s| + |p|))<\/p>\n<p>Space complexity: O(|s| + |p|)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, 14 ms\r\nclass Solution {\r\npublic:\r\n  bool isMatch(string s, string p) {\r\n    return isMatch(s.c_str(), p.c_str());\r\n  }\r\nprivate:\r\n  bool isMatch(const char* s, const char* p) {\r\n    if (*p == '\\0') return *s == '\\0';\r\n        \r\n    \/\/ normal case, e.g. 'a.b','aaa', 'a'\r\n    if (p[1] != '*' || p[1] == '\\0') {\r\n      \/\/ no char to match\r\n      if (*s == '\\0') return false;\r\n\r\n      if (*s == *p || *p == '.')\r\n        return isMatch(s + 1, p + 1);\r\n      else\r\n        return false;\r\n    }\r\n    else {\r\n      int i = -1;\r\n      while (i == -1 || s[i] == p[0] || p[0] == '.') {\r\n          if (isMatch(s + i + 1, p + 2)) return true;\r\n          if (s[++i] == '\\0') break;\r\n      }\r\n      return false;\r\n    }\r\n    \r\n    return false;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an input string (s) and a pattern (p), implement regular expression matching with support for\u00a0&#8216;.&#8217;\u00a0and\u00a0&#8216;*&#8217;. &#8216;.&#8217; Matches any single character. &#8216;*&#8217; Matches zero&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[370,44,47],"tags":[217,410,224,4],"class_list":["post-4007","post","type-post","status-publish","format-standard","hentry","category-programming-language","category-searching","category-string","tag-hard","tag-matching","tag-regex","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4007"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4007\/revisions"}],"predecessor-version":[{"id":4010,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4007\/revisions\/4010"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}