{"id":23,"date":"2018-06-06T00:00:56","date_gmt":"2018-06-06T00:00:56","guid":{"rendered":"https:\/\/sinyalee.com\/essays\/?p=23"},"modified":"2024-09-02T03:25:30","modified_gmt":"2024-09-02T03:25:30","slug":"leetcode-hard-10-regular-expression-matching","status":"publish","type":"post","link":"https:\/\/sinyalee.com\/essays\/?p=23","title":{"rendered":"[LeetCode HARD] 10 Regular Expression Matching"},"content":{"rendered":"<p>LeetCode has become the de facto go-to website for tech interviews. As a former competitive programmer, the LeetCode \u201chard\u201d, \u201cmedium\u201d, and \u201ceasy\u201d problems are more like \u201ceasy\u201d, \u201cvery easy\u201d, \u201cextremely easy\u201d for me. I think it would be a good idea for me to put my solutions of some of the LeetCode hard problems here. Hope it would be helpful for people who are preparing for tech interviews.<\/p>\n<p>This is my second LeetCode solution article, for\u00a0<a href=\"https:\/\/leetcode.com\/problems\/regular-expression-matching\/\">LeetCode Problem 10, Regular Expression Matching<\/a>.<\/p>\n<h3>Problem<\/h3>\n<p>Given an alphabet string and a simplified version of regular expression. Check if the string match the regular expression.<\/p>\n<p>In the simplified version of regular expressions, there are only three type of elements: (1) a letter, matching a letter, (2) a dot, matching any letter (3) a letter or a dot followed by a star, matching zero or more copies of that letter, or any letter if it\u2019s a dot.<\/p>\n<h3>Solution<\/h3>\n<p>This is a typical DP problem. Let\u00a0<em>f(i,j)<\/em>\u00a0be whether the prefix of length\u00a0<em>i<\/em>\u00a0of the string match the prefix of length\u00a0<em>j<\/em>\u00a0of the regular expression.<\/p>\n<p>The base case is f(0,0)=true.<\/p>\n<p>For the recursive step, if the\u00a0<em>j<\/em>-th element of is a letter or a dot, then we simply check if the\u00a0<em>i<\/em>-th element of the string matches and check if\u00a0<em>f(i-1, j-1)<\/em>\u00a0is true.<\/p>\n<p>If the\u00a0<em>j<\/em>-th element of is a star, then we check\u00a0<em>f(i, j-2)<\/em>\u00a0to see if we can match the string without that star, and then we check if the\u00a0<em>(i-1)<\/em>-th element matches and check\u00a0<em>f(j-1, j)<\/em>\u00a0is true.<\/p>\n<h3>Source Code<\/h3>\n<p>The source code is in C++. Instead of writing in a recursive manner, I wrote the program in the other direction because it\u2019s easier to write.<\/p>\n<pre><code class=\"language-cpp\">\nbool isMatch(string s, string p) {\n    std::vector&lt;std::vector &gt; \n        f(s.size()+1, std::vector(p.size()+1, false));\n    f[0][0] = true;\n    for (int i = 0; i &lt;= s.size(); ++i) \n        for (int j = 0; j &lt;= p.size(); ++j) \n            if (f[i][j]) {\n                if (i &lt; s.size() &amp;&amp; j &lt; p.size() \n                    &amp;&amp; (s[i] == p[j] || p[j] == &#039;.&#039;) )\n                    f[i+1][j+1] = true;\n                if (j &lt; p.size()-1 &amp;&amp; p[j+1] == &#039;*&#039;)\n                    f[i][j+2] = true;\n                if (i &lt; s.size() &amp;&amp; j &gt; 1 &amp;&amp; p[j-1] == &#039;*&#039; \n                    &amp;&amp; (p[j-2] == s[i] || p[j-2] == &#039;.&#039;) )\n                    f[i+1][j] = true;\n            }\n    return f[s.size()][p.size()];\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>LeetCode has become the de facto go-to website for tech interviews. As a former competitive programmer, the LeetCode \u201chard\u201d, \u201cmedium\u201d, and \u201ceasy\u201d problems are more like \u201ceasy\u201d, \u201cvery easy\u201d, \u201cextremely easy\u201d for me. I think it would be a good idea for me to put my solutions of some of the LeetCode hard problems here. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23"}],"version-history":[{"count":3,"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sinyalee.com\/essays\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}