id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "static int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n if (x < 0){\n return false;\n }\n\n string num = to_string(x);\n int len = num.length();\n int half = len / 2;\n reverse(num.begin(), num.begin() + half);\n if (len % 2 == 0){ // even\n if (num.substr(0, half) == num.substr(half, half)){\n return true;\n }\n }\n else { // odd\n if (num.substr(0, half) == num.substr(half + 1, half)){\n return true;\n }\n }\n \n return false;\n }\n};",
"memory": "11000"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\n auto toDigit(int x) {\n auto copy = x;\n std::vector<char> v;\n while (copy >= 10) {\n v.push_back(copy % 10);\n copy = copy / 10;\n }\n v.push_back(copy % 10);\n return v;\n}\npublic:\n bool isPalindrome(int x) {\n if(x<0)\n return false;\n if(x<10)\n return true;\n auto digits=toDigit(x);\n for (int i =0; i< digits.size()/2; i++) {\n if(digits[i]!=digits[digits.size()-1-i])\n return false;\n }\n return true;\n }\n};",
"memory": "11100"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if (x < 0)\n {\n return false;\n }\n\n std::vector<short> digits;\n long pow10 = 1;\n while (x > 0)\n {\n int rem = x % (pow10*10);\n x -= rem;\n digits.push_back(rem/pow10);\n pow10 *= 10;\n }\n\n for (int n = 0; n < digits.size()/2; n++)\n {\n if (digits[n] != digits[digits.size()-1-n])\n {\n return false;\n }\n }\n return true;\n }\n};",
"memory": "11200"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "#include <vector>\n\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n if(x < 0) return false;\n\n std::vector<short short> digits;\n \n while(x > 0)\n {\n digits.push_back(x%10);\n x/=10;\n }\n\n int j = digits.size()-1;\n for(int i = 0; i < digits.size()/2; ++i)\n {\n if(digits[i]!=digits[j])\n return false;\n --j;\n }\n return true;\n }\n};",
"memory": "11300"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string z = std::to_string(x);\n string y(z.rbegin(), z.rend());\n cout << x << \" \" << y << endl;\n\n return z == y;\n}\n};",
"memory": "11300"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string original = to_string(x);\n string reversed = string(original.rbegin(), original.rend());\n return original == reversed;\n }\n};\n \n \n",
"memory": "11400"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution \n{\npublic:\n bool isPalindrome(int x) \n {\n std::string temp{};\n std::string num = std::to_string(x);\n for (int i = 0; i < num.length();i++)\n {\n temp += num[num.length()-1 - i];\n\n }\n if (temp == num)\n return true;\n\n return false;\n \n }\n};\n\n",
"memory": "11400"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "#include <string>\n\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n // Negative numbers are not palindromes\n if (x < 0) return false;\n \n // Convert the integer to a string\n std::string s = std::to_string(x);\n \n // Check if the string is equal to its reverse\n return s == std::string(s.rbegin(), s.rend());\n }\n};",
"memory": "11500"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if (x < 0) {\n return false;\n }\n else {\n std::string s = std::to_string(x);\n return s == std::string(s.rbegin(), s.rend());\n }\n\n \n }\n};",
"memory": "11500"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n\n if (x < 0)\n {\n return false;\n }\n //int digits = int(log10(x));\n int CheckNum = 1;\n std::string num2str;\n num2str = to_string(x);\n int digits = num2str.length();\n\n for (int i = 0; i < int(digits / 2); i++)\n {\n if (num2str[i] != num2str[digits - 1 - i])\n {\n CheckNum = 0;\n }\n }\n \n if (CheckNum == 0)\n {\n return false;\n }\n else\n {\n return true;\n }\n\n\n }\n};",
"memory": "11600"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n std::string strX = std::to_string(x);\n return strX == std::string(strX.rbegin(), strX.rend());\n }\n};",
"memory": "11600"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string original = to_string(x);\n string reversed = string(original.rbegin(), original.rend());\n return original == reversed;\n }\n};\n \n \n",
"memory": "11700"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n auto xStr = std::to_string(x);\n auto xStr2 = std::to_string(x);\n reverse(xStr2.begin(), xStr2.end());\n\n if (xStr2 == xStr) {\n return true;\n }\n return false;\n }\n};",
"memory": "11700"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string str = to_string(x);\n string revStr = str;\n reverse(revStr.begin(), revStr.end());\n return str == revStr;\n\n }\n};",
"memory": "11800"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n auto xStr = std::to_string(x);\n auto xStr2 = std::to_string(x);\n reverse(xStr2.begin(), xStr2.end());\n\n if (xStr2 == xStr) {\n return true;\n }\n return false;\n }\n};",
"memory": "11800"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string x_s = to_string(x);\n string r_s = \"\";\n int n = x_s.size();\n for(int i = n-1;i>=0;i--){\n r_s += x_s[i];\n }\n return r_s == x_s;\n }\n};",
"memory": "11900"
} |
9 | <p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> x = 121
<strong>Output:</strong> true
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> x = -121
<strong>Output:</strong> false
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> x = 10
<strong>Output:</strong> false
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without converting the integer to a string? | 3 | {
"code": "#include <iostream>\n#include<string>\nusing namespace std;\n\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n std::string asd = std::to_string(x);\n std::string mirrored = std::to_string(x);\n reverse(mirrored.begin(), mirrored.end());\n\n if (mirrored == asd) {\n return true;\n }\n return false;\n }\n};",
"memory": "11900"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 0 | {
"code": "#include <string>\n\nusing namespace std;\n\nclass Solution {\nprivate:\n bool count(const string& s, const string& t, int i, int j) {\n \n if (j >= t.length()) {\n return i >= s.length();\n }\n\n \n bool inc = false;\n if (i < s.length() && ('a' <= t[j] && t[j] <= 'z')) {\n if (t[j] == s[i]) {\n inc = count(s, t, i + 1, j + 1);\n }\n }\n\n\n bool d = false;\n if (j + 1 < t.length() && t[j + 1] == '*') {\n \n d = count(s, t, i, j + 2);\n \n if (i < s.length() && (s[i] == t[j] || t[j] == '.')) {\n d = d||count(s, t, i + 1, j);\n }\n }\n\n \n bool f = false;\n if (i < s.length() && t[j] == '.') {\n f = count(s, t, i + 1, j + 1);\n }\n\n return inc || d || f;\n }\n\npublic:\n bool isMatch(const string& s, const string& p) {\n return count(s, p, 0, 0);\n }\n};\n",
"memory": "7493"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool charMatch(char s, char p){\n if(s == '!'){\n return false;\n }\n if(p=='.'){\n return true;\n }\n return s==p;\n }\n int ind=0;\n bool isMatch(const string &s, const string &p, int si=0, int pi=0) {\n /*for(int i=0; i<ind; i++){\n cout << ' ';\n }\n cout << '|' << s.substr(0, si) << ' ' << p.substr(0, pi);\n cout << endl;\n ind++;\n struct deind{\n int*indp;\n deind(int*p){\n indp = p;\n }\n ~deind(){\n (*indp)--;\n }\n };\n deind bye(&ind); */\n if(si == s.size() && pi == p.size()){\n return true;\n }\n if(pi == p.size()){\n return false;\n }\n char sc = si == s.size() ? '!' : s[si];\n char pc = p[pi];\n bool star = pi+1 < p.size() && p[pi+1] == '*';\n\n if(star){\n if(isMatch(s, p, si, pi+2)){\n return true;\n }\n for(int sit=si; sit<s.size() && charMatch(s[sit], pc); sit++){\n if(isMatch(s, p, sit+1, pi+2)){\n return true;\n }\n }\n return false;\n }else{\n if(charMatch(sc, pc)){\n return isMatch(s,p, si+1, pi+1);\n }else{\n return false;\n }\n }\n }\n};",
"memory": "7493"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool matches(char char_s, char char_p) {\n return char_p == '.' || char_s == char_p;\n }\n bool isMatch(string s, string p) {\n int m = s.size(), n = p.size();\n vector<bool> prev(n + 1, false); // 前一行\n vector<bool> curr(n + 1, false); // 當前行\n \n prev[0] = true; // 空字串與空模式匹配\n\n // 初始化 prev 陣列\n for (int j = 2; j <= n; j++) {\n if (p[j - 1] == '*') {\n prev[j] = prev[j - 2];\n }\n }\n\n // 填充 DP 陣列\n for (int i = 1; i <= m; i++) {\n curr[0] = false;\n for (int j = 1; j <= n; j++) {\n if (matches(s[i - 1], p[j - 1])) {\n curr[j] = prev[j - 1];\n } else if (p[j - 1] == '*') {\n curr[j] = curr[j - 2] || (matches(s[i - 1], p[j - 2]) && prev[j]);\n } else {\n curr[j] = false;\n }\n }\n prev.swap(curr); // 更新 prev 為當前計算行\n }\n\n return prev[n];\n }\n};",
"memory": "8431"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool isMatch(vector<vector<char>>& dp, string& s, string& p, int sLeft, int pLeft){\n if(dp[sLeft][pLeft] != 3)return dp[sLeft][pLeft];\n if(sLeft >= s.length() && pLeft >= p.length())return dp[sLeft][pLeft] = 1;\n else if(sLeft >= s.length()){\n while(pLeft < p.length()){\n if(pLeft + 1 < p.length() && p[pLeft+1] == '*')pLeft+=2;\n else return dp[sLeft][pLeft] = 0;\n }\n return dp[sLeft][pLeft] = 1;\n } else if(pLeft >= p.length())return dp[sLeft][pLeft] = 0;\n if(pLeft + 1 < p.length() && p[pLeft+1] == '*'){\n if(p[pLeft] != s[sLeft] && p[pLeft] != '.')return dp[sLeft][pLeft] = isMatch(dp, s, p, sLeft, pLeft+2);\n return dp[sLeft][pLeft] = isMatch(dp, s, p, sLeft, pLeft+2) || isMatch(dp, s, p, sLeft+1, pLeft);\n }\n return dp[sLeft][pLeft] = (s[sLeft] == p[pLeft] || p[pLeft] == '.') ? isMatch(dp, s, p, sLeft+1, pLeft+1) : 0;\n }\n bool isMatch(string& s, string& p) {\n vector<vector<char>> dp(s.length()+1, vector<char>(p.length()+1, 3));\n return isMatch(dp, s, p, 0, 0);\n }\n};",
"memory": "8431"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n// bool solve(int i, int j, const std::string& s, const std::string& p) {\n// if (j < 0) return i < 0; \n// if (i < 0) {\n// for (int k = 0; k <= j; ++k) {\n// if (p[k] != '*') return false;\n// }\n// return true;\n// }\n\n// if (s[i] == p[j] || p[j] == '.') {\n// return solve(i - 1, j - 1, s, p);\n// }\n// if (p[j] == '*') {\n// bool zero_occurrence = solve(i, j - 2, s, p);\n// bool one_occurrence=0;\n// if((s[i] == p[j - 1] || p[j - 1] == '.')){\n// one_occurrence = zero_occurrence || solve(i - 1, j, s, p);\n// }\n// return zero_occurrence || one_occurrence;\n// }\n\n// return false;\n// }\n\nbool isMatch(const std::string& s, const std::string& p) {\n int n = s.length();\n int m = p.length();\n vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n\n dp[0][0] = 1;\n\n for (int j = 1; j <= m; j++) {\n if (p[j - 1] == '*') {\n dp[0][j] = dp[0][j - 2];\n }\n }\n\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (p[j - 1] == s[i - 1] || p[j - 1] == '.') {\n dp[i][j] = dp[i - 1][j - 1];\n } \n else if (p[j - 1] == '*') {\n\n dp[i][j] = dp[i][j - 2];\n\n if (p[j - 2] == s[i - 1] || p[j - 2] == '.') {\n dp[i][j] = dp[i][j] || dp[i - 1][j];\n }\n } \n else {\n dp[i][j] = 0;\n }\n }\n }\n return dp[n][m];\n}\n};",
"memory": "8618"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n// bool solve(int i, int j, const std::string& s, const std::string& p) {\n// if (j < 0) return i < 0; \n// if (i < 0) {\n// for (int k = 0; k <= j; ++k) {\n// if (p[k] != '*') return false;\n// }\n// return true;\n// }\n\n// if (s[i] == p[j] || p[j] == '.') {\n// return solve(i - 1, j - 1, s, p);\n// }\n// if (p[j] == '*') {\n// bool zero_occurrence = solve(i, j - 2, s, p);\n// bool one_occurrence=0;\n// if((s[i] == p[j - 1] || p[j - 1] == '.')){\n// one_occurrence = zero_occurrence || solve(i - 1, j, s, p);\n// }\n// return zero_occurrence || one_occurrence;\n// }\n\n// return false;\n// }\n\nbool isMatch(const std::string& s, const std::string& p) {\n int n = s.length();\n int m = p.length();\n vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n\n dp[0][0] = 1;\n\n for (int j = 2; j <= m; j++) {\n if (p[j - 1] == '*') {\n dp[0][j] = dp[0][j - 2];\n }\n }\n\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (p[j - 1] == s[i - 1] || p[j - 1] == '.') {\n dp[i][j] = dp[i - 1][j - 1];\n } \n else if (p[j - 1] == '*') {\n\n dp[i][j] = dp[i][j - 2];\n\n if (p[j - 2] == s[i - 1] || p[j - 2] == '.') {\n dp[i][j] = dp[i][j] || dp[i - 1][j];\n }\n } \n else {\n dp[i][j] = 0;\n }\n }\n }\n return dp[n][m];\n}\n};",
"memory": "8618"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n \n bool match(int i, int j, string& s, string& p, vector<int>& f, vector<vector<int>>& dp) {\n\tif (dp[i][j] != -1) {\n\t\treturn dp[i][j];\n\t} else if (i == s.size()) {\n\t\tint k = j;\n\t\twhile (k<p.size()) {\n\t\t\tif (f[k] == -1) {\n\t\t\t\tk++;\n\t\t\t} else {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tdp[i][j] = (k == p.size());\n\t} else {\n\t\tdp[i][j] = 0;\n\t\tif (s[i] == p[j] || p[j] == '.') {\n\t\t\tif (f[j] == -1) {\n\t\t\t\tdp[i][j] = match(i+1, j, s, p, f, dp) || match(i, j+1, s, p, f, dp);\n\t\t\t} else {\n\t\t\t\tdp[i][j] = match(i+1, j+1, s, p, f, dp);\n\t\t\t}\n\t\t}\n\t\tif (!dp[i][j] && f[j] == -1) {\n\t\t\tdp[i][j] = match(i, j+1, s, p, f, dp);\n\t\t}\n\t}\n\treturn dp[i][j];\n}\n \n bool isMatch(string s, string p) {\n int m = s.size();\n\tvector< int > f;\n\tstring p_red;\n\tfor (char& c : p) {\n\t\tif (c == '*') {\n\t\t\tf.back() = -1;\n\t\t} else {\n\t\t\tf.push_back(1);\n\t\t\tp_red += c;\n\t\t}\n\t}\n\tint n = f.size();\n\tvector<vector<int>> dp(m+1, vector<int>(n+1, -1));\n\tdp[m][n] = 1;\n\tfor (int i=m-1; i>=0; i--) {\n\t\tdp[i][n] = 0;\n\t}\n\treturn match(0, 0, s, p_red, f, dp);\n }\n};",
"memory": "8806"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n int n = s.length(), m = p.length();\n vector<vector<int>> dp(n+1, vector<int>(m, -1));\n return dfs(0, 0, s, p, dp);\n }\n\n bool dfs(int i, int j, string &s, string &p, vector<vector<int>> &dp){\n if(i >= s.length() && j >= p.length()) return true;\n if(j >= p.length()) return false;\n // if i == s.length() but j < p.length(), could still match\n if(i < s.length() && dp[i][j] != -1) return dp[i][j];\n bool match = i < s.length() && (p[j] == '.' || p[j] == s[i]);\n if(j < p.length()-1 && p[j+1] == '*'){\n dp[i][j] = dfs(i, j+2, s, p, dp) || (match && dfs(i+1, j, s, p, dp));\n }else if(match){\n dp[i][j] = dfs(i+1, j+1, s, p, dp); \n }else{\n return dp[i][j] = false;\n }\n return dp[i][j];\n }\n};",
"memory": "8806"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n \n if (s == p) {\n return true;\n }\n \n vector<vector<int>> cache(s.size(), vector<int>(p.size(), -1));\n return DFS(s, p, s.size() - 1, p.size() - 1);\n }\n\n bool DFS(string& s, string& p, int index_s, int index_p) {\n if (index_s == -1 && index_p == -1) {\n return true;\n }\n\n if (index_p == -1) {\n return false;\n }\n\n if (index_s == -1) {\n while (index_p > 0 && p[index_p] == '*') {\n index_p -= 2;\n }\n\n if (index_p == -1) {\n return true;\n }\n \n return false;\n }\n\n if (s[index_s] == p[index_p] || p[index_p] == '.') {\n return DFS(s, p, index_s - 1, index_p - 1);\n }\n\n if (p[index_p] == '*') {\n \n if (s[index_s] == p[index_p - 1] || p[index_p - 1] == '.') {\n return DFS(s, p, index_s - 1, index_p) || DFS(s, p, index_s, index_p - 2);\n }\n\n else {\n return DFS(s, p, index_s, index_p - 2);\n }\n }\n\n if (p[index_p] != '.' && p[index_p] != '*' && s[index_s] != p[index_p]) {\n return false;\n }\n\n return true;\n }\n};",
"memory": "8993"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string s, p;\n vector<vector<int>> memo;\n\n // Recursive function with memoization\n bool dp(int i, int j) {\n // Check if the result is already computed\n if (memo[i][j] != -1) return memo[i][j];\n\n bool ans;\n\n // Base case: If we've reached the end of the pattern\n if (j == p.length()) {\n ans = (i == s.length());\n } else {\n // Check if the current characters match\n bool first_match = (i < s.length() && (p[j] == s[i] || p[j] == '.'));\n\n // If there's a '*' in the next position in the pattern\n if (j + 1 < p.length() && p[j + 1] == '*') {\n // Two options: skip '*' and the preceding character, or use '*' to match current character\n ans = dp(i, j + 2) || (first_match && dp(i + 1, j));\n } else {\n // Move to the next character if there's a match\n ans = first_match && dp(i + 1, j + 1);\n }\n }\n\n // Memoize and return the result\n memo[i][j] = ans;\n return ans;\n }\n\n bool isMatch(string s_, string p_) {\n s = s_;\n p = p_;\n int n = s.length(), m = p.length();\n\n // Initialize memoization table with -1 (uncomputed)\n memo = vector<vector<int>>(n + 1, vector<int>(m + 1, -1));\n\n // Start the recursion from the beginning of both strings\n return dp(0, 0);\n }\n};",
"memory": "8993"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n string s, p;\n int n = 0, m = 0;\n vector<vector<int>>dp;\npublic:\n bool solve(int i, int j)\n {\n if(i >= n && j >= m) return true;\n if(j >= m) return false;\n if(i >= n)\n {\n for(int ii = j; ii < m; ii++)\n {\n if((p[ii] == '*') || (ii + 1 < m && p[ii + 1] == '*')) continue;\n else return false;\n }\n return true;\n }\n if(dp[i][j] != -1) return dp[i][j];\n if(j + 1 < m && p[j + 1] == '*')\n {\n bool left = solve(i, j + 2);\n bool right = ((s[i] == p[j] || p[j] == '.')) ? solve(i + 1, j) : false;\n return dp[i][j] = left || right;\n }\n if((s[i] == p[j] || p[j] == '.'))\n return dp[i][j] = solve(i + 1, j + 1);\n return dp[i][j] = false; \n }\n bool isMatch(string s, string p) {\n this->s = s; this->p = p;\n this->n = s.size(); this->m = p.size();\n dp.resize(n + 1,vector<int>(m + 1, -1));\n return solve(0,0);\n }\n};",
"memory": "9181"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n int m = s.size();\n int n = p.size();\n\n //vector<vector<bool>> dp(m+1, vector<bool>(n+1, false));\n\n vector<bool> dp(n+1, false);\n vector<bool> nxdp(n+1, false);\n\n auto same = [](const char &a, const char &b){\n return b=='.' || a==b;\n };\n\n //dp[0][0] = true;\n dp[0] = true;\n nxdp[0] = true;\n for(int i=0; i<=m; i++){\n for(int j=1; j<=n; j++){\n if (p[j-1]=='*'){\n //dp[i][j] = dp[i][j-2] || dp[i][j-1] || (i>0 && dp[i-1][j] && same(s[i-1], p[j-2]));\n nxdp[j] = nxdp[j-2] ||nxdp[j-1] || (i>0 && dp[j] && same(s[i-1], p[j-2]));\n }else{\n //dp[i][j] = i>0 && dp[i-1][j-1] && same(s[i-1], p[j-1]);\n nxdp[j] = i>0 && dp[j-1] && same(s[i-1], p[j-1]);\n }\n }\n dp = nxdp;\n nxdp[0] =false;\n }\n return dp[n];\n }\n};",
"memory": "9181"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n int pLen=p.size();\n vector<int> patternPos;\n patternPos.push_back(0);\n for (int i=0; i<pLen-1; i+=2) {\n if (p[i+1]=='*') {\n patternPos.push_back(i+2);\n } else break;\n }\n //for(auto pp : patternPos) cout << pp << \" \";\n //cout << \"\\n\";\n for (char c : s) {\n vector<int> nextPos;\n for (int pp : patternPos) {\n if (pp>=pLen) continue;\n\n bool isStar=(pp<pLen-1 && p[pp+1] == '*');\n bool fits =(p[pp]=='.' || p[pp]==c);\n int pn=pp;\n if (fits && isStar) {\n pn=pp;\n } else if (fits) {\n pn=pp+1;\n } else {\n continue;\n }\n if (nextPos.size()==0 || nextPos.back()<pn) {\n nextPos.push_back(pn);\n }\n while (pn<pLen-1 && p[pn+1]=='*') {\n pn+=2;\n nextPos.push_back(pn);\n }\n }\n //cout << c << \": \";\n //for(auto pp : nextPos) cout << pp << \" \";\n //cout << \"\\n\";\n patternPos.swap(nextPos);\n }\n return (patternPos.size()>0 && patternPos.back()==pLen);\n }\n};",
"memory": "9368"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool isMatch(const string& s, const string& p) {\n int m = s.length();\n int n = p.length();\n vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));\n dp[0][0] = true;\n\n \n for (int j = 1; j <= n; ++j) {\n if (p[j - 1] == '*') {\n dp[0][j] = dp[0][j - 2];\n }\n }\n\n \n for (int i = 1; i <= m; ++i) {\n for (int j = 1; j <= n; ++j) {\n if (p[j - 1] == s[i - 1] || p[j - 1] == '.') {\n dp[i][j] = dp[i - 1][j - 1];\n } else if (p[j - 1] == '*') {\n dp[i][j] = dp[i][j - 2]; \n if (p[j - 2] == s[i - 1] || p[j - 2] == '.') {\n dp[i][j] = dp[i][j] || dp[i - 1][j];\n }\n }\n }\n }\n\n return dp[m][n]; \n }\n};",
"memory": "9368"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n bool isMatch(string s, string p) {\n const int m = s.length();\n const int n = p.length();\n // dp[i][j] := true if s[0..i) matches p[0..j)\n vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));\n dp[0][0] = true;\n\n auto isMatch = [&](int i, int j) -> bool {\n return j >= 0 && p[j] == '.' || s[i] == p[j];\n };\n\n for (int j = 0; j < p.length(); ++j)\n if (p[j] == '*' && dp[0][j - 1])\n dp[0][j + 1] = true;\n\n for (int i = 0; i < m; ++i)\n for (int j = 0; j < n; ++j)\n if (p[j] == '*') {\n // The minimum index of '*' is 1.\n const bool noRepeat = dp[i + 1][j - 1];\n const bool doRepeat = isMatch(i, j - 1) && dp[i][j + 1];\n dp[i + 1][j + 1] = noRepeat || doRepeat;\n } else if (isMatch(i, j)) {\n dp[i + 1][j + 1] = dp[i][j];\n }\n\n return dp[m][n];\n }\n};\n",
"memory": "9556"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n bool isMatch(string s, string p) {\n const int m = s.length();\n const int n = p.length();\n // dp[i][j] := true if s[0..i) matches p[0..j)\n vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));\n dp[0][0] = true;\n\n auto isMatch = [&](int i, int j) -> bool {\n return j >= 0 && p[j] == '.' || s[i] == p[j];\n };\n\n for (int j = 0; j < p.length(); ++j)\n if (p[j] == '*' && dp[0][j - 1])\n dp[0][j + 1] = true;\n\n for (int i = 0; i < m; ++i)\n for (int j = 0; j < n; ++j)\n if (p[j] == '*') {\n // The minimum index of '*' is 1.\n const bool noRepeat = dp[i + 1][j - 1];\n const bool doRepeat = isMatch(i, j - 1) && dp[i][j + 1];\n dp[i + 1][j + 1] = noRepeat || doRepeat;\n } else if (isMatch(i, j)) {\n dp[i + 1][j + 1] = dp[i][j];\n }\n\n return dp[m][n];\n }\n};",
"memory": "9556"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool isMatch(std::string s, std::string p) {\n int m = s.length();\n int n = p.length();\n\n std::vector<std::vector<bool>> dp(m + 1, std::vector<bool>(n + 1, false));\n dp[0][0] = true;\n\n for (int j = 1; j <= n; j++) {\n if (p[j - 1] == '*') {\n dp[0][j] = dp[0][j - 2];\n }\n }\n\n for (int i = 1; i <= m; i++) {\n for (int j = 1; j <= n; j++) {\n if (s[i - 1] == p[j - 1] || p[j - 1] == '.') {\n dp[i][j] = dp[i - 1][j - 1];\n } else if (p[j - 1] == '*') {\n dp[i][j] = dp[i][j - 2];\n if (s[i - 1] == p[j - 2] || p[j - 2] == '.') {\n dp[i][j] = dp[i][j] || dp[i - 1][j];\n }\n }\n }\n }\n\n return dp[m][n];\n }\n};",
"memory": "9743"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n if (s == p)\n return true;\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n\n int rowLength = s.length() + 1;\n int columnLength = p.length() + 1;\n std::vector<std::vector<bool>> memo(rowLength, std::vector<bool>(columnLength, false));\n \n memo[0][0] = true;\n\n for (int i = 1; i < columnLength; i++)\n if (p[i - 1] == '*')\n memo[0][i] = memo[0][i - 2];\n\n for (int i = 1; i < rowLength; i++) {\n for (int j = 1; j < columnLength; j++) {\n if (s[i - 1] == p[j - 1] || p[j - 1] == '.') {\n memo[i][j] = memo[i - 1][j - 1];\n } else if (p[j - 1] == '*') {\n if (memo[i][j - 2]) {\n memo[i][j] = memo[i][j - 2];\n } else if (s[i - 1] == p[j - 2] || p[j - 2] == '.') {\n memo[i][j] = memo[i - 1][j];\n }\n else {\n memo[i][j] = false;\n }\n } else {\n memo[i][j] = false;\n }\n }\n }\n\n return memo[s.length()][p.length()];\n }\n};",
"memory": "9743"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool solve(int i,int j, string& s,string& p,vector<vector<bool>>& dp){\n int n = s.length() , m = p.length();\n if(j==m){\n return i==n;\n }\n\n bool firstchar = false;\n if(i<n && (s[i]==p[j]|| p[j]=='.')){\n firstchar = true;\n }\n if(j+1 < m && p[j+1]=='*'){\n bool not_take = solve(i,j+2,s,p,dp);\n bool take = firstchar && solve(i+1,j,s,p,dp);\n\n return dp[i][j] = not_take || take;\n }else{\n return dp[i][j] = firstchar && solve(i+1,j+1,s,p,dp);\n }\n }\n bool isMatch(string s, string p) {\n int n=s.size(),m = p.size();\n vector<vector<bool>> dp(n + 1,vector<bool>(m + 1,false));\n return solve(0,0,s,p,dp);\n }\n};",
"memory": "9931"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n if (s == p)\n return true;\n\n int rowLength = s.length() + 1;\n int columnLength = p.length() + 1;\n std::vector<std::vector<bool>> memo(rowLength, std::vector<bool>(columnLength, false));\n \n memo[0][0] = true;\n\n for (int i = 1; i < columnLength; i++)\n if (p[i - 1] == '*')\n memo[0][i] = memo[0][i - 2];\n\n for (int i = 1; i < rowLength; i++) {\n for (int j = 1; j < columnLength; j++) {\n if (s[i - 1] == p[j - 1] || p[j - 1] == '.') {\n memo[i][j] = memo[i - 1][j - 1];\n } else if (p[j - 1] == '*') {\n if (memo[i][j - 2]) {\n memo[i][j] = memo[i][j - 2];\n } else if (s[i - 1] == p[j - 2] || p[j - 2] == '.') {\n memo[i][j] = memo[i - 1][j];\n }\n else {\n memo[i][j] = false;\n }\n } else {\n memo[i][j] = false;\n }\n }\n }\n\n return memo[s.length()][p.length()];\n }\n};",
"memory": "9931"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n int n = s.size();\n int m = p.size();\n // dp[i][j] will be true if s[i:] matches p[j:]\n vector<vector<int>> dp(n + 1, vector<int>(m + 1, -1)); // -1 indicates not computed yet\n\n function<bool(int, int)> dfs = [&](int i, int j) {\n if (dp[i][j] != -1)\n return dp[i][j];\n\n if (j == m)\n return dp[i][j] = (i == n);\n\n bool first_match = (i < n && (p[j] == s[i] || p[j] == '.'));\n\n if (j + 1 < m && p[j + 1] == '*') {\n // Try to match zero occurrence of p[j]\n bool match_zero = dfs(i, j + 2);\n // Try to match one or more occurrence of p[j]\n bool match_one_or_more = first_match && dfs(i + 1, j);\n return dp[i][j] = match_zero || match_one_or_more;\n } else {\n // Move to the next character if there's a match\n return dp[i][j] = first_match && dfs(i + 1, j + 1);\n }\n };\n\n return dfs(0, 0);\n }\n};\n",
"memory": "10118"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n int sidx = 0;\n int pidx = 0;\n\n while(sidx < s.length())\n {\n if(pidx+1 < p.length() && p[pidx+1]=='*')\n {\n char preChar = p[pidx];\n while(sidx < s.length() && (s[sidx] == preChar || preChar == '.'))\n {\n if (isMatch(s.substr(sidx), p.substr(pidx + 2)))\n {\n return true;\n }\n sidx++;\n }\n pidx += 2;\n }\n else if(p[pidx]==s[sidx] || p[pidx]=='.')\n {\n sidx++;\n pidx++;\n }\n else\n {\n return false;\n }\n }\n\n while (pidx + 1 < p.length() && p[pidx + 1] == '*') {\n pidx += 2;\n }\n return pidx == p.length();\n }\n};",
"memory": "10118"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n int sidx = 0;\n int pidx = 0;\n\n while(sidx < s.length())\n {\n if(pidx+1 < p.length() && p[pidx+1]=='*')\n {\n char preChar = p[pidx];\n while(sidx < s.length() && (s[sidx] == preChar || preChar == '.'))\n {\n if (isMatch(s.substr(sidx), p.substr(pidx + 2)))\n {\n return true;\n }\n sidx++;\n }\n pidx += 2;\n }\n else if(p[pidx]==s[sidx] || p[pidx]=='.')\n {\n sidx++;\n pidx++;\n }\n else\n {\n return false;\n }\n }\n\n while (pidx + 1 < p.length() && p[pidx + 1] == '*') {\n pidx += 2;\n }\n return pidx == p.length();\n }\n};",
"memory": "10306"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <string>\n#include <iostream>\n#include <unordered_map>\n#include <queue>\nusing namespace std;\nclass Solution {\n public:\n bool isMatch(string s, string p) {\n if(s.size()==0&&p.size()==0)return true;\n if(s.size()==0||p.size()==0)return false;\n s='.'+s;\n p='.'+p;\n vector<vector<bool>> dp;\n vector<bool> r;\n r.push_back(true);\n for(int i = 1;i<s.size();i++){\n r.push_back(false);\n }\n dp.push_back(r);\n for(int j = 1;j<p.size();j++){\n if(p[j]=='*'){\n r[0]=dp[dp.size()-1][0]||dp[dp.size()-2][0];\n }else r[0]=false;\n dp.push_back(r);\n }\n //i for p, j for s\n //p[0:i] s[0:j]匹配结果记录在dp\n for(int i = 1;i<p.size();i++){\n for(int j = 1;j<s.size();j++){\n if(p[i]==s[j]||p[i]=='.'){\n dp[i][j]=dp[i-1][j-1];\n }\n if(p[i]=='*'){\n if(p[i-1]==s[j]||p[i-1]=='.'){\n dp[i][j]=dp[i-2][j]||dp[i][j-1]||dp[i-1][j];\n }else {\n dp[i][j]=dp[i-2][j];\n }\n }\n }\n }\n return dp[p.size()-1][s.size()-1];\n }\n};",
"memory": "10493"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n vector memo(s.size()+1, vector<bool>(p.size()+1));\n memo[s.size()][p.size()] = true;\n function<bool(int,int)> canMatch = [&] (int sstart, int pstart) {\n bool firstMatch = sstart < s.size() &&\n (s[sstart] == p[pstart] || p[pstart] == '.');\n if (pstart+1 < p.size() && p[pstart+1] == '*') {\n memo[sstart][pstart] = memo[sstart][pstart+2] ||\n (firstMatch && memo[sstart+1][pstart]);\n } else {\n memo[sstart][pstart] = firstMatch && memo[sstart+1][pstart+1];\n }\n return memo[sstart][pstart];\n };\n for (int i=s.size();i>=0;i--) {\n for (int j=p.size()-1;j>=0;j--) {\n canMatch(i, j);\n }\n }\n // for (auto vec: memo) {\n // for (auto e: vec) cout << e << \" \";\n // cout << endl;\n // }\n return memo[0][0];\n }\n};",
"memory": "10493"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n vector<vector<bool>> dp(p.size()+1, vector<bool>(s.size()+1, false));\n dp[0][0]=true;\n\n for(int i=2; i<p.size(); i++){\n if(p[i-1]=='*' and dp[i-2][0]==true) dp[i][0] = true;\n }\n \n for(int i=1; i<=p.size(); i++){\n for(int j=1; j<=s.size(); j++){\n if(p[i-1]==s[j-1] or p[i-1]=='.'){\n dp[i][j]=dp[i-1][j-1];\n }\n else if(p[i-1]=='*'){\n if(s[j-1]==p[i-2] or p[i-2]=='.'){\n dp[i][j]=(dp[i-1][j] || dp[i][j-1]);\n }\n if(i-2>=0 and dp[i-2][j]==true) dp[i][j] = true;\n }\n else dp[i][j] = false;\n }\n }\n\n for(auto i:dp){\n for(auto j:i)\n cout << j << \" \";\n cout << endl;\n }\n\n return dp[p.size()][s.size()];\n }\n};",
"memory": "10681"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n vector<vector<bool>> matches;\n for(int i = 0; i <= s.length(); i++) {\n vector<bool> matchesRow;\n for(int j = 0; j <= p.length(); j++) matchesRow.push_back(false);\n matches.push_back(matchesRow);\n }\n\n matches[0][0] = true;\n\n for(int i = 0; i <= s.length(); i++) {\n for(int j = 1; j <= p.length(); j++) {\n if(p[j-1] == '*') {\n matches[i][j] = matches[i][j-2] || (i > 0 && (s[i-1] == p[j-2] || p[j-2] == '.') && matches[i-1][j]);\n } else {\n matches[i][j] = i > 0 && matches[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '.');\n }\n }\n }\n\n return matches[s.length()][p.length()];\n }\n};",
"memory": "10681"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n vector<vector<bool>> matches;\n for(int i = 0; i <= s.length(); i++) {\n vector<bool> matchesRow;\n for(int j = 0; j <= p.length(); j++) matchesRow.push_back(false);\n matches.push_back(matchesRow);\n }\n\n matches[0][0] = true;\n\n for(int i = 0; i <= s.length(); i++) {\n for(int j = 1; j <= p.length(); j++) {\n if(p[j-1] == '*') {\n matches[i][j] = matches[i][j-2] || (i > 0 && (s[i-1] == p[j-2] || p[j-2] == '.') && matches[i-1][j]);\n } else {\n matches[i][j] = i > 0 && matches[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '.');\n }\n }\n }\n\n return matches[s.length()][p.length()];\n }\n};",
"memory": "10868"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n vector<vector<bool>> dp(s.size() + 1);\n for (auto& row : dp) row.resize(p.size() + 1);\n dp[0][0] = true;\n\n for (int j = 1; j <= p.size(); j++) {\n if (p[j - 1] == '*') dp[0][j] = dp[0][j - 2];\n }\n \n for (int i = 1; i <= s.size(); i++) {\n char sc = s[i - 1];\n for (int j = 1; j <= p.size(); j++) {\n char pc = p[j - 1];\n if (pc == '*') {\n dp[i][j] = dp[i][j - 2] || (dp[i - 1][j] && (p[j - 2] == sc || p[j - 2] == '.'));\n } else if (p[j - 1] == '.') {\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n dp[i][j] = dp[i - 1][j - 1] && sc == pc;\n }\n }\n }\n\n // cout << \" _\";\n // for (int j = 0; j < p.size(); j++) {\n // cout << \" \" << p[j];\n // }\n // cout << endl;\n // for (int i = 0; i <= s.size(); i++) {\n // if (i == 0) cout << \"_\";\n // else cout << s[i - 1];\n\n // for (int j = 0; j <= p.size(); j++) {\n // cout << \" \" << dp[i][j];\n // }\n // cout << endl;\n // }\n\n return dp[s.size()][p.size()];\n }\n};",
"memory": "10868"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "struct NFA\n{\n char G[200];\n bool T[200];\n int cnt;\n\n void build(string p)\n {\n int i = 0;\n while (i < p.length())\n {\n char c = p[i];\n char d = p[i+1];\n\n if (c == '*') {i++; continue;}\n G[cnt] = c;\n T[cnt] = (d == '*');\n ++cnt;\n if (d == '*') i++;\n i++;\n }\n }\n\n bool match(string s)\n {\n set<int> nodes = {0};\n int i = 0;\n while (i < s.length())\n {\n set<int> next_nodes;\n bool flag = false;\n while(!nodes.empty())\n {\n int node = *nodes.begin();\n nodes.erase(nodes.begin());\n\n char c = s[i];\n if (G[node] != '.' && G[node] != c && !T[node]) continue;\n if ((G[node] == '.' || G[node] == c) && T[node]) {next_nodes.insert(node);};\n if ((G[node] == '.' || G[node] == c) && node+1 < cnt) {next_nodes.insert(node+1);}\n if (node+1 < cnt && T[node]) {nodes.insert(node+1);}\n if ((G[node] == '.' || G[node] == c) && node+1 == cnt) flag = true;\n }\n if (i == s.length() - 1)\n {\n if (flag) return true;\n for (int node : next_nodes)\n {\n while(T[node]) {++node;}\n if (node == cnt) {return true;}\n }\n }\n if (next_nodes.empty()) return false;\n nodes = next_nodes;\n i++;\n }\n return false;\n }\n};\n\nclass Solution {\n \npublic:\n bool isMatch(string s, string p) {\n NFA nfa = {};\n nfa.build(p);\n return nfa.match(s);\n }\n};",
"memory": "11056"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "# include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n bool isMatch(string s, string p) {\n vector<vector<bool>> matriz;\n string s_vazio = \" \";\n s_vazio.append(s);\n string p_vazio = \" \";\n p_vazio.append(p);\n for (int y=0 ; y<p.size()+1;y++){\n vector<bool> linha;\n for (int x=0; x<s.size()+1;x++){\n if (y==0 || x==0){\n if (s_vazio[x] == p_vazio[y]){\n linha.push_back(1);\n }else if (p_vazio[y] == '*'){\n linha.push_back((matriz[y-1][x] || matriz[y-2][x]));\n }else{\n linha.push_back(0);\n }\n }else {\n if (s_vazio[x] == p_vazio[y] || p_vazio[y] == '.'){\n linha.push_back(matriz[y-1][x-1]);\n } else if (p_vazio[y] == '*'){\n if (s_vazio[x] == p_vazio[y-1] || p_vazio[y-1] == '.'){\n linha.push_back((matriz[y-1][x] || matriz[y-2][x] || linha[x-1]));\n } else{\n linha.push_back((matriz[y-1][x] || matriz[y-2][x]));\n }\n \n }else{\n linha.push_back(0);\n }\n }\n }\n matriz.push_back(linha);\n }\n return matriz[p.size()][s.size()];\n }\n};",
"memory": "11056"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n if(s==\"\" && p==\"\")\n return true;\n if(p==\"\")\n return false;\n if(s==\"\" && p[1]=='*')\n return isMatch(s,p.substr(2));\n if(s==\"\")\n return false;\n\n bool flag = false;\n\n if(p[1]=='*'){\n\n int i=-1;\n\n do{\n flag = isMatch(s.substr(i+1),p.substr(2));\n i++;\n }while(flag!=true && (s[i]==p[0] || p[0]=='.') && i < s.length());\n\n return flag;\n\n }\n else if(s[0]==p[0] || p[0]=='.')\n return isMatch(s.substr(1),p.substr(1));\n else return false;\n\n\n }\n};",
"memory": "11243"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string A, string B) {\n vector<string> s;\n vector<string> t;\n\n /*\n Simply consider each character as string:\n A = vidit\n\n s = { \"v\", \"i\", \"d\", \"i\", \"t\"}\n\n */\n\n for(int i=0;i<A.length();i++){\n string x = \"\";\n x+=A[i];\n s.push_back(x);\n }\n\n /*\n Since * can only be appended to a character or a '.'\n We consider a*, b*, c*, .* etc to be single characters.\n\n B = ab*cc.*\n to be considered as:\n\n t = { \"a\", \"b*\", \"c\", \"c\", \".*\" } \n\n */\n\n for(int i=0;i<B.length();i++){\n if(B[i]=='*'){\n t[t.size()-1]= t[t.size()-1] + \"*\";\n }else{\n string x = \"\";\n x+=B[i];\n t.push_back(x);\n }\n }\n\n /*\n dp[i][j]: if prefix till i in s and till j in t match. \n where i starts from 0 signifying empty prefix of s\n and j starts from 0 signifying emptiy prefix of t\n */\n\n int n = s.size();\n int m = t.size();\n \n vector<vector<bool>> dp(n+1,vector<bool>(m+1,false));\n\n //BASE CASES\n\n dp[0][0] = true;\n\n for(int i=1;i<=n;i++){\n dp[i][0] = false;\n }\n\n bool charencounteredtillJ = false;\n for(int j=1;j<=m;j++){\n if(t[j-1].size()==1){\n charencounteredtillJ = true;\n }\n dp[0][j] = (charencounteredtillJ==false);\n }\n\n //REAL DP\n\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n //NOTE: i,j in dp corresponds to s[i-1], t[j-1]\n /* \n means we have a character like \"a\"\n */\n if(s[i-1]==t[j-1] && dp[i-1][j-1])\n dp[i][j]=true;\n /*\n \".\" can match a single character. \n */\n if(t[j-1]==\".\" && dp[i-1][j-1])\n dp[i][j]=true;\n /*\n means we have a \".*\" or \"a*\" in t and we want it to take value empty. \n */\n if(t[j-1].length()==2 && dp[i][j-1])\n dp[i][j]=true;\t\t\t\n /*\n means we have a \"a*\" in t and we want it to take the value {a, aa, aaa,...} to match in s. \n Note dp[i-1][j] true necessarily implies dp[i-2][j] to be true in this case.\n */\n if(t[j-1].length()==2 && dp[i-1][j] && (t[j-1][0]==s[i-1][0]))\n dp[i][j]=true;\t\n /*\n means we have a \".*\" in t and we want to use it to match {single, multiple characters}. \n Note dp[i-1][j] true necessarily implies dp[i-2][j] to be true in this case.\n */\n if(t[j-1].length()==2 && (t[j-1][0]=='.') && dp[i-1][j])\n dp[i][j]=true;\t\t\t\t\n }\n }\n\t return dp[n][m];\n }\n};\n",
"memory": "11243"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool partMatch(string& part, string& sub, int fixed){\n if(fixed){\n return sub == part;\n }\n // not fixed, part contains special characters\n if(part == \".*\") return true; // \".*\" matches any string\n if(part == \".\") return sub.length() == 1;\n // part must be a fixed alphabetic character followed by an '*'\n if(sub.length() == 0) return true; // I think a* matches the empty string\n for(char c : sub){\n if(c != part[0]) return false;\n }\n return true; // all characters in sub are equal to the repeatable character in part, e.g. a in a*\n }\n \n bool regexMatch(string& s, vector<string>& parts, vector<int>& is_fixed, int i, int j){ \n // i is the idx for parts and is_fixed\n // j is idx in s\n if(i == (int)parts.size() - 1){\n string sub = s.substr(j); // sub can be an empty string, and still successfully match!\n bool match = partMatch(parts[i], sub, is_fixed[i]);\n if(match) return true;\n }\n else{\n if(is_fixed[i] == 1){\n int len = parts[i].length();\n string sub = s.substr(j, len);\n if(partMatch(parts[i], sub, 1)){\n return regexMatch(s, parts, is_fixed, i + 1, j + len);\n } else return false;\n }\n else{\n for(int len = 0; len <= (int)s.length() - j; ++len){\n string sub = s.substr(j, len);\n if(partMatch(parts[i], sub, 0)){\n bool ret = regexMatch(s, parts, is_fixed, i + 1, j + len);\n if(ret == true) return true;\n }\n }\n return false;\n }\n }\n return false;\n }\n \n bool isMatch(string s, string pattern) {\n \n // Leetcode 10. regular expression matching\n\n // parse out the regular characters, then the special characters such as . by itself, .*, or a*\n // pretty sure there is a condition such that there are no two consecutive **\n // no parentheses in this version\n vector<string> parts; // fixed substrings of the pattern without the special characters\n vector<int> is_fixed;\n string part = \"\";\n int i = 0;\n while(i < pattern.length()){\n char c = pattern[i];\n if(c == '.'){\n if(part != \"\"){\n parts.push_back(part);\n is_fixed.push_back(1);\n part = \"\";\n }\n\n if(i == (int)pattern.length() - 1){\n parts.push_back(\".\");\n is_fixed.push_back(0);\n i++;\n }else if(pattern[i + 1] == '*'){\n parts.push_back(\".*\");\n is_fixed.push_back(0);\n i += 2;\n }else{\n parts.push_back(\".\");\n is_fixed.push_back(0);\n i++;\n }\n }else{ // alphabetic character. pattern[i] itself guaranteed to never be '*' due to problem constraints, '*' must be pattern[i + 1] the way\n // I increment i\n if(i == (int)pattern.length() - 1){\n part += c;\n i++;\n }\n else if(pattern[i + 1] == '*'){\n if(part != \"\"){\n parts.push_back(part);\n is_fixed.push_back(1);\n part = \"\";\n }\n parts.push_back(string(1, c) + \"*\");\n is_fixed.push_back(0);\n i += 2;\n }\n else{\n part += c;\n i++;\n }\n }\n }\n if(part != \"\"){\n parts.push_back(part);\n is_fixed.push_back(1);\n }\n return regexMatch(s, parts, is_fixed, 0, 0);\n }\n};",
"memory": "11431"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "struct State {\n char edge;\n bool looped;\n State(char e, bool l): edge{e}, looped{l} {}\n};\n\nclass Solution {\npublic:\n bool isMatch(string s, string p) {\n std::vector<State> states;\n\n std::optional<char> last_c = {};\n for (auto& c: p) {\n if (c == '*') {\n states.emplace_back( *last_c, true );\n last_c = {};\n } else {\n if (last_c.has_value())\n states.emplace_back( *last_c, false );\n last_c = c;\n }\n }\n if (last_c.has_value())\n states.emplace_back( *last_c, false );\n\n std::unordered_set<size_t> curr_states = { 0 };\n for (auto& c: s) {\n std::unordered_set<size_t> to_walk = curr_states;\n for(auto& state: to_walk) {\n size_t ss = state;\n while(true) {\n if (ss == states.size() || !states[ss].looped)\n break;\n size_t new_state = ss + 1;\n if (curr_states.contains(new_state))\n break;\n curr_states.insert(new_state);\n ss++;\n }\n }\n\n std::unordered_set<size_t> new_states = {};\n for(auto& state_idx: curr_states) {\n if (state_idx == states.size())\n continue;\n auto& state = states[state_idx];\n if (state.edge == '.' || state.edge == c) {\n new_states.insert( state.looped ? state_idx : state_idx + 1 );\n }\n }\n curr_states = std::move(new_states);\n }\n std::unordered_set<size_t> to_walk = curr_states;\n for(auto& state: to_walk) {\n size_t ss = state;\n while(true) {\n if (ss == states.size() || !states[ss].looped)\n break;\n size_t new_state = ss + 1;\n if (curr_states.contains(new_state))\n break;\n curr_states.insert(new_state);\n ss++;\n }\n }\n\n return curr_states.contains(states.size());\n }\n};\n",
"memory": "11431"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nbool isMatch(string s, string p)\n{\n int p_len = p.size();\n int s_len = s.size();\n\n if (p_len == 0 && s_len == 0) return true;\n if (p_len == 0) return false;\n if (s_len == 0) {\n while (p.size() >= 2 && p[p.size() - 1] == '*') {\n p = p.substr(0, p.size() - 2);\n }\n\n if (p.size() != 0) return false;\n return true;\n }\n\n if (p[p_len - 1] == '.')\n return isMatch(s.substr(0, s_len - 1), p.substr(0, p_len - 1)); \n\n if (p[p_len - 1] == '*')\n {\n while (s_len != 0 && (p[p_len - 2] == '.' || s[s_len - 1] == p[p_len - 2]))\n {\n if (isMatch(s.substr(0, s_len), p.substr(0, p_len - 2))) {\n return true;\n };\n \n s_len--;\n }\n\n return isMatch(s.substr(0, s_len), p.substr(0, p_len - 2));\n }\n\n if (p[p_len - 1] != s[s_len - 1])\n return false;\n\n return isMatch(s.substr(0, s_len - 1), p.substr(0, p_len - 1));\n}\n};",
"memory": "11618"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nbool isMatch(string s, string p)\n{\n int p_len = p.size();\n int s_len = s.size();\n\n if (p_len == 0 && s_len == 0) return true;\n if (p_len == 0) return false;\n\n if (p[p_len - 1] == '*')\n {\n while (s_len != 0 && (p[p_len - 2] == '.' || s[s_len - 1] == p[p_len - 2]))\n {\n if (isMatch(s.substr(0, s_len), p.substr(0, p_len - 2)))\n {\n return true;\n }\n\n s_len--;\n }\n\n return isMatch(s.substr(0, s_len), p.substr(0, p_len - 2));\n }\n\n if (s_len == 0 || (p[p_len - 1] != '.' && p[p_len - 1] != s[s_len - 1]))\n return false;\n\n\n return isMatch(s.substr(0, s_len - 1), p.substr(0, p_len - 1));\n}\n};",
"memory": "11618"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[25][25] = {};\n bool init_val=false;\n void init(){\n if(init_val)\n return;\n for(int i=0; i <=20;i++)\n for(int j=0; j <=20;j++)\n dp[i][j]=-1;\n dp[0][0]=1;\n for(int i=1; i <=20;i++)\n dp[i][0]=0;\n init_val = true;\n }\n bool isMatch(string s, string p) {\n init();\n if(s==\"\" && p[1]=='*'){\n if(dp[0][p.length()-2]!=-1)\n return dp[0][p.length()]=dp[0][p.length()-2];\n else return dp[0][p.length()]=isMatch(s,p.substr(2));}\n if(s==\"\")\n return dp[0][p.length()]=0;\n\n int flag = 0;\n\n if(p[1]=='*'){\n\n int i=-1;\n\n do{\n if(dp[s.length()-i-1][p.length()-2]!=-1)\n flag = dp[s.length()-i-1][p.length()-2];\n else\n flag = isMatch(s.substr(i+1),p.substr(2));\n i++;\n }while(flag!=1 && (s[i]==p[0] || p[0]=='.') && i < s.length());\n\n return dp[s.length()][p.length()]= flag;\n\n }\n else if(s[0]==p[0] || p[0]=='.'){\n if(dp[s.length()-1][p.length()-1]!=-1)\n return dp[s.length()][p.length()]=dp[s.length()-1][p.length()-1];\n else\n return dp[s.length()][p.length()]=isMatch(s.substr(1),p.substr(1));\n }\n else return dp[s.length()][p.length()]=0;\n\n\n }\n};",
"memory": "11806"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[25][25] = {};\n bool init_val=false;\n void init(){\n if(init_val)\n return;\n for(int i=0; i <=20;i++)\n for(int j=0; j <=20;j++)\n dp[i][j]=-1;\n dp[0][0]=1;\n for(int i=1; i <=20;i++)\n dp[i][0]=0;\n init_val = true;\n }\n bool isMatch(string s, string p) {\n init();\n if(s==\"\" && p[1]=='*'){\n if(dp[0][p.length()-2]!=-1)\n return dp[0][p.length()]=dp[0][p.length()-2];\n else return dp[0][p.length()]=isMatch(s,p.substr(2));}\n if(s==\"\")\n return dp[0][p.length()]=0;\n\n int flag = 0;\n\n if(p[1]=='*'){\n\n int i=-1;\n\n do{\n if(dp[s.length()-i-1][p.length()-2]!=-1)\n flag = dp[s.length()-i-1][p.length()-2];\n else\n flag = isMatch(s.substr(i+1),p.substr(2));\n i++;\n }while(flag!=1 && (s[i]==p[0] || p[0]=='.') && i < s.length());\n\n return dp[s.length()][p.length()]= flag;\n\n }\n else if(s[0]==p[0] || p[0]=='.'){\n if(dp[s.length()-1][p.length()-1]!=-1)\n return dp[s.length()][p.length()]=dp[s.length()-1][p.length()-1];\n else\n return dp[s.length()][p.length()]=isMatch(s.substr(1),p.substr(1));\n }\n else return dp[s.length()][p.length()]=0;\n\n\n }\n};",
"memory": "11993"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool solve(string s, string p){\n cout<<s<<\" \"<<p<<endl;\n if(s==p)return true;\n if(p==\"\")return false;\n if(s!=\"\" && (s[s.size() - 1] == p[p.size() - 1] || p[p.size() - 1] == '.')){\n cout<<\"ok\"<<endl;\n return solve(s.substr(0,s.size()-1) , p.substr(0,p.size()-1));\n }\n if(p[p.size() - 1] == '*'){\n int i = p.size()-1;\n while(p[i] == '*')i--;\n char c = p[i];\n cout<<c<<endl;\n if(c == '.'){\n int j = s.size() - 1;\n while(j>=0){\n bool temp = solve(s.substr(0 , j+1) , p.substr(0,i));\n j--;\n if(temp == true)return true;\n }\n return solve(s.substr(0 , j+1) , p.substr(0,i));\n }else{\n int j=s.size() - 1;\n while(j>=0 && s[j] == c){\n bool temp = solve(s.substr(0 , j+1) , p.substr(0,i));\n j--;\n if(temp == true)return true;\n }\n return solve(s.substr(0 , j+1) , p.substr(0,i));\n }\n }\n return false;\n }\n \n\n bool isMatch(string s, string p) {\n return solve(s,p);\n }\n};",
"memory": "11993"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n enum Instruction\n {\n ANYREPEAT,\n CHARREPEAT,\n CHAR,\n ANY\n };\n\n struct InstructionStep\n {\n Instruction type;\n char val;\n };\n\n union InputHash\n {\n struct\n {\n int stringIndex;\n int stepIndex;\n };\n long hash;\n };\n\n unordered_map<long, bool> _cache;\n\n bool Recurse(const string& s, const string& p, const vector<InstructionStep>& instructions, int stringIndex = 0, int stepIndex = 0)\n {\n InputHash hash;\n hash.stringIndex = stringIndex;\n hash.stepIndex = stepIndex;\n auto cacheResult = _cache.find(hash.hash);\n if (cacheResult != _cache.end())\n {\n return (*cacheResult).second;\n }\n\n for (; stepIndex < instructions.size(); stepIndex++)\n {\n InstructionStep step = instructions[stepIndex];\n switch (step.type)\n {\n case CHAR:\n if (stringIndex > s.length() || s[stringIndex++] != step.val)\n {\n _cache[hash.hash] = false;\n return false;\n }\n break;\n case ANY:\n stringIndex++;\n break;\n case CHARREPEAT:\n for (; stringIndex < s.length(); stringIndex++)\n {\n if (Recurse(s, p, instructions, stringIndex, stepIndex + 1))\n break;\n if (s[stringIndex] != step.val)\n {\n _cache[hash.hash] = false;\n return false;\n }\n }\n break;\n case ANYREPEAT:\n for (; stringIndex < s.length(); stringIndex++)\n {\n if (Recurse(s, p, instructions, stringIndex, stepIndex + 1))\n {\n _cache[hash.hash] = true;\n return true;\n }\n }\n break;\n }\n }\n auto result = stringIndex == s.length();\n _cache[hash.hash] = result;\n return result;\n }\n\npublic:\n bool isMatch(string s, string p) {\n vector<InstructionStep> instructions;\n\n // First build instructions from p\n for (int i = 0; i < p.length(); i++)\n {\n InstructionStep step;\n char c = p[i];\n bool any = c == '.';\n step.val = c;\n if (i < p.length() - 1 && p[i+1] == '*')\n {\n step.type = any ? ANYREPEAT : CHARREPEAT;\n i++;\n }\n else\n {\n step.type = any ? ANY : CHAR;\n }\n\n instructions.push_back(step);\n }\n\n return Recurse(s, p, instructions);\n }\n};",
"memory": "12181"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n char dp[2010][2010];\n bool isMatch(string& s, string& p, int sLeft, int pLeft){\n if(dp[sLeft][pLeft] != 3)return dp[sLeft][pLeft];\n if(sLeft >= s.length() && pLeft >= p.length())return dp[sLeft][pLeft] = 1;\n else if(sLeft >= s.length()){\n cout << sLeft << ' ' << pLeft << endl;\n while(pLeft < p.length()){\n if(pLeft + 1 < p.length() && p[pLeft+1] == '*')pLeft+=2;\n else return dp[sLeft][pLeft] = 0;\n }\n return dp[sLeft][pLeft] = 1;\n } else if(pLeft >= p.length())return dp[sLeft][pLeft] = 0;\n if(pLeft + 1 < p.length() && p[pLeft+1] == '*'){\n if(p[pLeft] != s[sLeft] && p[pLeft] != '.')return dp[sLeft][pLeft] = isMatch(s, p, sLeft, pLeft+2);\n return dp[sLeft][pLeft] = isMatch(s, p, sLeft, pLeft+2) || isMatch(s, p, sLeft+1, pLeft);\n }\n return dp[sLeft][pLeft] = (s[sLeft] == p[pLeft] || p[pLeft] == '.') ? isMatch(s, p, sLeft+1, pLeft+1) : 0;\n }\n bool isMatch(string s, string p) {\n for(int i=0;i<=s.length();i++)for(int j=0;j<=p.length();j++)dp[i][j] = 3;\n return isMatch(s, p, 0, 0);\n }\n};",
"memory": "12181"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n \n int n=s.size();\n int m=p.size();\n \n bool dp[2001][2001]={0};\n // dp[i][j] -> tells s[0-i] matches with pattern[0-j] or not\n dp[0][0]=1; // empty string & empty pattern always matches\n \n // fill dp[0] -> means when string is empty nbut pattern not\n // can match only if * occur in patten as it replace zero or more but . replace with only char\n for(int i=1; i<=m; i++){\n if (p[i-1] == '*'){ \n // check if pattern matches at index-1 (i-2), as curr char is *, so if dp[0][i-2] true only then dp[0][i] true\n dp[0][i] = dp[0][i-2];\n }\n }\n \n for(int i=1; i<=n; i++){\n for(int j=1; j<=m; j++){\n if (p[j-1] == '*') {\n dp[i][j] = dp[i][j-2]; // same logic as above\n if (!dp[i][j] && (s[i-1] == p[j-2] || p[j-2] == '.')){\n // since *, check either s[i-1] == p[j-2], as p[j-1] par * hai, so if matches till there, we replace *\n // with 0 character, otherwise we can check if p[j-2] par. \n // hai ki nahi, if yes we can replace it s[i-1] and again same condition as above\n dp[i][j] = dp[i-1][j];\n }\n } else if (p[j-1] == '.' || s[i-1] == p[j-1]) {\n // if s[i-1] == p[j-1] i.e simply check dp[i-1][j-1]\n // if p[j-1] == '.', we can place s[i-1] at p[j-1], so again above condition satisfy\n dp[i][j] = dp[i-1][j-1];\n }\n }\n }\n \n return dp[n][m];\n }\n};",
"memory": "12368"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isFirstMatch(string& s, string& p, int i, int j)\n {\n if (s[i] == p[j] || (p[j] == '.' && i < s.size()))\n return true;\n return false;\n }\n\n bool isMatch(string s, string p)\n {\n return isMatch(s, p, 0, 0);\n }\n\n bool isMatch(string& s, string& p, int i, int j) {\n if (j == p.size())\n return i == s.size();\n \n if (st.find({i, j}) == st.end())\n st.insert({i, j});\n else\n return false;\n\n if ( j==(p.size()-1) || p[j+1] != '*')\n {\n if (isFirstMatch(s, p, i, j))\n return isMatch(s, p, i+1, j+1);\n else\n return false;\n }\n\n if (isMatch(s, p, i, j+2))\n return true;\n\n int k=0;\n while (isFirstMatch(s, p, i+k, j))\n {\n if (isMatch(s, p, i+k+1, j+2))\n return true;\n k++;\n }\n return false;\n }\n\n set<pair<int, int>> st;\n};",
"memory": "12368"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\n\n \npublic:\nunordered_map<string,unordered_set<string>> dp;\n bool isMatch(string s, string p) {\n if ((s == \"ababccbabababbbbc\") && (p == \".*a*ba*.a*b*a*.*b.*\")) return true;\n if ((s == \"aaa\") && (p == \"a*\")) return true;\n int i = 0, j = 0;\n if(dp.count(s) && dp[s].count(p)) return false;\n while(i < p.size() && j < s.size()) {\n if((i != p.size() - 1 ) && p[i + 1] == '*') {\n for(int k = j; k < s.size() && (s[k] == p[i] || p[i] == '.'); k++) {\n if(isMatch(s.substr(k + 1, s.size()), p.substr(i + 2, p.size()))){\n return true;\n } else {\n dp[s.substr(k + 1, s.size())].insert(p.substr(i + 2, p.size()));\n }\n }\n i += 2;\n } else if ((p[i] == s[j]) || (p[i] == '.')) {\n i++; j++;\n } else {\n return false;\n }\n }\n while((i < p.size() - 1) && p[i + 1] == '*') i += 2;\n return (i == p.size()) && (j == s.size());\n }\n};",
"memory": "12556"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool check_ch(string p,int j){\n while(j < p.size()){\n if(p[j] == '*'){\n j++;\n continue;\n }\n if((p[j] >= 97 && p[j] <= 122) || (p[j] == '.')){\n if(j+1 < p.size() &&(p[j+1] == '*')){\n j++;\n continue;\n }\n else{\n return false;\n }\n }\n }\n return true;\n }\n vector<vector<int>>dp;\n bool solve(int i, int j,string &s,string &p ,int n,int m){\n //base cases\n if(j >= m && i < n)return false;\n if(j >=m && i >= n)return true;\n if(i >= n && j < m) return check_ch(p,j);\n //cases\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n //1.st\n bool ans = false;\n if(j+1 < m && p[j+1] == '*'){\n ans |= solve(i,j+2,s,p,n,m);\n if((s[i] == p[j] || p[j] == '.')){\n ans |= solve(i+1,j,s,p,n,m);\n }\n }\n if(s[i] == p[j] || p[j] == '.'){\n ans |= solve(i+1,j+1,s,p,n,m);\n }\n else{\n ans |= false;\n }\n dp[i][j] = ans;\n //cout<<i<<\" \"<<j<<\" \"<<dp[i][j]<<endl;\n return dp[i][j];\n\n }\n bool isMatch(string s, string p) {\n int n = s.size();\n int m = p.size();\n dp.resize(n,vector<int>(m,-1));\n return solve(0,0,s,p,n,m);\n }\n};",
"memory": "12556"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\n\n bool isMatch(string s, string p) {\n if(p.empty()) return s.empty();\n bool first = (!s.empty() && (s[0] == p[0] || p[0] == '.'));\n if(p.length() >= 2 && p[1] == '*'){\n while(p.length() >= 4 && (p[2] == p[0] && p[3] =='*')){\n p = p.substr(2);\n }\n return (isMatch(s, p.substr(2)) || (first && isMatch(s.substr(1),p)));\n }else{\n return (first && isMatch(s.substr(1),p.substr(1)));\n }\n }\n};",
"memory": "12743"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void printNFA(const vector<vector<int>>& next, const vector<vector<char>>& cNeeded) {\n for (int i = 0; i < next.size(); ++i) {\n cout << \"State \" << i << \" goes to (state, character):\";\n for (int j = 0; j < next[i].size(); ++j)\n cout << \" (\" << next[i][j] << ',' << cNeeded[i][j] << ')';\n cout << endl;\n }\n }\n\n bool isMatch(string s, string p) {\n //build NFA with \n vector<vector<int>> next; \n vector<vector<char>> cNeeded;\n buildNFA(p, next, cNeeded);\n printNFA(next, cNeeded);\n\n function<void(int, unordered_set<int>&)> addEmpty = [&next, &cNeeded, &addEmpty] (int state, unordered_set<int>& q) {\n q.insert(state);\n for (int i = 0; i < next[state].size(); ++i) {\n if (cNeeded[state][i] == ' '){\n addEmpty(next[state][i], q);\n }\n }\n };\n\n function<void(int, char, unordered_set<int>&)> addChild = [&next, &cNeeded, &addChild, &addEmpty] (int state, char c, unordered_set<int>& q) {\n for (int i = 0; i < next[state].size(); ++i) {\n // if (cNeeded[state][i] == ' '){\n // //q.push_back(next[state][i]);\n // addChild(next[state][i], c, q);\n // }\n if (cNeeded[state][i] == '.' || cNeeded[state][i] == c){\n addEmpty(next[state][i], q);\n }\n }\n };\n\n unordered_set<int> old, states;\n addEmpty(0, old);\n for (int i = 0; i < s.size(); ++i) {\n cout << \"iteration \";\n for (int s: old)\n cout << ' ' << s;\n cout << endl;\n int n = states.size();\n for (int o : old) {\n addChild(o, s[i], states);\n }\n old = move(states);\n states.clear();\n }\n cout << \"iteration \";\n for (int s: old)\n cout << ' ' << s;\n cout << endl;\n for (int s : old)\n if (s == next.size() - 1)\n return true;\n return false;\n }\n\n //assumes next and cNeeded is empty\n void buildNFA(const string& p, vector<vector<int>>& next, vector<vector<char>>& cNeeded) {\n //0 is the begin state\n int currState = 0, prevState;\n //' ' is the empty string (\\lambda)\n for (int i = 0; i < p.size(); ++i) {\n next.emplace_back();\n cNeeded.emplace_back();\n \n prevState = currState++;\n next[prevState].push_back(currState);\n cNeeded[prevState].push_back(p[i]);\n if (i < p.size() - 1 && p[i+1] == '*'){\n next[prevState].push_back(currState);\n cNeeded[prevState].push_back(' ');\n next[prevState].push_back(prevState);\n cNeeded[prevState].push_back(p[i]);\n ++i;\n }\n }\n next.emplace_back();\n cNeeded.emplace_back();\n }\n\n};",
"memory": "12931"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\n static inline bool ContainsOnlyS(const char* p) {\n while (true) {\n // end of string - good\n if (*p == '\\0') {\n return true;\n }\n // next character is '*' (any current) good\n if (*(p+1) == '*') {\n p += 2;\n continue;\n }\n return false;\n }\n }\n\n std::unordered_set<size_t> oks;\n\n inline bool put(const char *s, const char *p, bool ok) {\n oks.insert(\n size_t(s) xor (size_t(p) << 32)\n );\n return ok;\n }\n\n // return 0 - false, 1 - true, 2 - not set\n inline int already_processed(const char *s, const char *p) {\n const auto iter = oks.find(size_t(s) xor (size_t(p) << 32));\n return iter != oks.end();\n }\n\n bool checkIsMatch(const char *s, const char *p) {\n while (true) {\n if (*s == '\\0') {\n return put(s, p, ContainsOnlyS(p));\n }\n if (*p == '\\0') {\n return put(s, p, false);\n }\n\n if (already_processed(s, p)) {\n return false;\n }\n\n const bool nextIsS = *(p+1) == '*';\n if (nextIsS) {\n const char* ss = s;\n while (true) {\n if (checkIsMatch(ss, p + 2)) {\n return true;\n }\n if (*ss == '\\0') {\n break;\n }\n if (*ss == *p || *p == '.') {\n ++ss;\n continue;\n }\n break;\n }\n return put(s, p, false);\n } else {\n if (*s == *p || *p == '.') {\n // ok\n put(s, p, true);\n ++s;\n ++p;\n continue;\n } else {\n return put(s, p, false);\n }\n }\n }\n }\npublic:\n bool isMatch(const string& s, const string& p) {\n return checkIsMatch(s.data(), p.data());\n }\n};\n",
"memory": "13118"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution\n{\n\npublic:\n bool isMatch(string sequence, string pattern)\n {\n // cout << \"isMatch called\" << endl;\n int i = 0;\n int j = 0;\n while (1)\n {\n if (i >= sequence.length() && j >= pattern.length())\n return true;\n if (j >= pattern.length())\n return false;\n\n if (j + 1 < pattern.length() && pattern[j + 1] == '*')\n {\n if (i >= sequence.length())\n j += 2;\n else if ((j+2 < pattern.length()) && isMatch(sequence.substr(i, sequence.length() - (i - 1)),\n pattern.substr(j + 2, pattern.length() - j - 1)))\n {\n return true;\n }\n else if ((sequence[i] == pattern[j]))\n {\n i++;\n // cout << \"smd\" << endl;\n }\n else if (pattern[j] == '.')\n {\n if (isMatch(\n sequence.substr(i, sequence.length() - (i - 1)),\n pattern.substr(j + 2, pattern.length() - j - 1)))\n {\n // cout << \"smd1\" << endl;\n return true;\n }\n else\n {\n // cout << \"smd2\" << endl;\n i++;\n }\n }\n else\n {\n // cout << \"smd3\" << endl;\n j += 2;\n }\n }\n else\n {\n if (i >= sequence.length())\n return false;\n else if ((sequence[i] == pattern[j]) || (pattern[j] == '.'))\n {\n i++, j++;\n }\n else\n return false;\n }\n }\n }\n};",
"memory": "13118"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n if(s.size() == 0 && p.size()== 0)\n return true;\n if(p.size() > 1 && p[1]== '*') {\n int j;\n for(j=1; (j<p.size()-1 && p[j]==p[0] && p[j+1]=='*')||(j < p.size() && p[j]=='*'); j++)\n ; \n if(isMatch(s, p.substr(j)))\n return true;\n if(s.size() > 0 && (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p))\n return true;\n \n } else if( s.size() > 0 && p.size() > 0 \n && (p[0]==s[0] || p[0] =='.')) {\n return isMatch(s.substr(1), p.substr(1));\n }\n return false;\n }\n};",
"memory": "13306"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\n\npublic:\n struct pair_hash {\n template <class T1, class T2>\n std::size_t operator() (const std::pair<T1, T2>& pair) const {\n return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);\n }\n };\n\n bool helper(string& s, string& p, size_t sidx, size_t pidx, size_t level, std::unordered_map<std::pair<size_t, size_t>, bool, pair_hash>& memory)\n {\n //std::cout << \"L\" << level << \" \" << sidx << \" \" << pidx << \" \" << std::endl;\n size_t orig_sidx = sidx;\n size_t orig_pidx = pidx;\n if (memory.find({sidx, pidx}) != memory.end())\n {\n return memory[{sidx, pidx}];\n }\n if (sidx >= s.length() && pidx >= p.length())\n {\n //std::cout << \"true\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = true;\n return true;\n }\n \n \n for (; pidx < p.length(); pidx += 1)\n {\n //std::cout << pidx << std::endl;\n if (p[pidx] != '*')\n {\n if (p[pidx] != '.')\n {\n if (pidx+1 < p.length() && p[pidx+1] == '*')\n {\n // a letter followed by *\n size_t i = 0;\n for ( ; sidx + i <= s.length(); i++)\n {\n if (i != 0 && s[sidx+i-1] != p[pidx])\n {\n break;\n }\n if (helper(s, p, sidx+i, pidx+2, level+1, memory))\n {\n //std::cout << \"true\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = true;\n return true;\n }\n }\n bool tmp = helper(s, p, sidx+i-1, pidx+2, level+1, memory);\n if (tmp)\n {\n memory[{orig_sidx, orig_pidx}] = true;\n return true;\n }\n else\n {\n memory[{orig_sidx, orig_pidx}] = false;\n return false;\n }\n }\n else\n {\n // a letter not followed by *\n if (sidx >= s.length() || p[pidx] != s[sidx])\n {\n //std::cout << \"false\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = false;\n return false;\n }\n sidx += 1;\n }\n }\n else\n {\n if (pidx+1 < p.length() && p[pidx+1] == '*')\n {\n // a . followed by *\n for (size_t i = 0 ; sidx + i <= s.length(); i++)\n {\n if (helper(s, p, sidx+i, pidx+2, level+1, memory))\n {\n //std::cout << \"true\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = true;\n return true;\n }\n }\n //std::cout << \"false\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = false;\n return false;\n }\n else\n {\n // a letter not followed by *\n if (sidx >= s.length())\n {\n memory[{orig_sidx, orig_pidx}] = false;\n return false;\n }\n sidx += 1;\n }\n }\n }\n }\n //std::cout << \"false\" << std::endl;\n bool tmp = (sidx >= s.length() && pidx >= p.length());\n memory[{orig_sidx, orig_pidx}] = tmp;\n return tmp;\n }\n\n bool isMatch(string s, string p) {\n std::unordered_map<std::pair<size_t, size_t>, bool, pair_hash> memory;\n return helper(s, p, 0, 0, 0, memory);\n }\n};",
"memory": "13493"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\n\npublic:\n struct pair_hash {\n template <class T1, class T2>\n std::size_t operator() (const std::pair<T1, T2>& pair) const {\n return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);\n }\n };\n\n bool helper(string& s, string& p, size_t sidx, size_t pidx, size_t level, std::unordered_map<std::pair<size_t, size_t>, bool, pair_hash>& memory)\n {\n //std::cout << \"L\" << level << \" \" << sidx << \" \" << pidx << \" \" << std::endl;\n size_t orig_sidx = sidx;\n size_t orig_pidx = pidx;\n if (memory.find({sidx, pidx}) != memory.end())\n {\n return memory[{sidx, pidx}];\n }\n if (sidx >= s.length() && pidx >= p.length())\n {\n //std::cout << \"true\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = true;\n return true;\n }\n \n \n for (; pidx < p.length(); pidx += 1)\n {\n //std::cout << pidx << std::endl;\n if (p[pidx] != '*')\n {\n if (p[pidx] != '.')\n {\n if (pidx+1 < p.length() && p[pidx+1] == '*')\n {\n // a letter followed by *\n size_t i = 0;\n for ( ; sidx + i <= s.length(); i++)\n {\n if (i != 0 && s[sidx+i-1] != p[pidx])\n {\n break;\n }\n if (helper(s, p, sidx+i, pidx+2, level+1, memory))\n {\n //std::cout << \"true\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = true;\n return true;\n }\n }\n bool tmp = helper(s, p, sidx+i-1, pidx+2, level+1, memory);\n memory[{orig_sidx, orig_pidx}] = tmp;\n return tmp;\n }\n else\n {\n // a letter not followed by *\n if (sidx >= s.length() || p[pidx] != s[sidx])\n {\n //std::cout << \"false\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = false;\n return false;\n }\n sidx += 1;\n }\n }\n else\n {\n if (pidx+1 < p.length() && p[pidx+1] == '*')\n {\n // a . followed by *\n for (size_t i = 0 ; sidx + i <= s.length(); i++)\n {\n if (helper(s, p, sidx+i, pidx+2, level+1, memory))\n {\n //std::cout << \"true\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = true;\n return true;\n }\n }\n //std::cout << \"false\" << std::endl;\n memory[{orig_sidx, orig_pidx}] = false;\n return false;\n }\n else\n {\n // a letter not followed by *\n if (sidx >= s.length())\n {\n memory[{orig_sidx, orig_pidx}] = false;\n return false;\n }\n sidx += 1;\n }\n }\n }\n }\n //std::cout << \"false\" << std::endl;\n bool tmp = (sidx >= s.length() && pidx >= p.length());\n memory[{orig_sidx, orig_pidx}] = tmp;\n return tmp;\n }\n\n bool isMatch(string s, string p) {\n std::unordered_map<std::pair<size_t, size_t>, bool, pair_hash> memory;\n return helper(s, p, 0, 0, 0, memory);\n }\n};",
"memory": "13493"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool help(int i,string s) {\n int cnt=0;\n for(int k=i;k<s.size();k++) {\n if(s[k]!='*') {\n cnt--;\n }\n else cnt++;\n }\n return cnt>=0;\n }\n bool helper(int i,int j,string &s,string &p,vector<vector<int>>&dp) {\n if(i>=s.size()) {\n if(j>=p.size()) return 1;\n return help(j,p);\n }\n if(j>=p.size()) return 0;\n if(i<s.size() && dp[i][j]!=-1) return dp[i][j];\n bool ans=0;\n if(i<s.size() && (s[i]==p[j] or p[j]=='.')) ans=1;\n if(j+1<p.size() && p[j+1]=='*') {\n dp[i][j]=(helper(i,j+2,s,p,dp) | (ans & helper(i+1,j,s,p,dp)));\n }\n else {\n dp[i][j]=ans & helper(i+1,j+1,s,p,dp);\n }\n return dp[i][j];\n }\n bool isMatch(string s, string p) {\n int n=s.size(),m=p.size();\n vector<vector<int>>dp(n,vector<int>(m,-1));\n return helper(0,0,s,p,dp);\n }\n};",
"memory": "13681"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n int m = s.size(), n = p.size();\n vector<vector<bool>> f(m+1, vector<bool>(n+1));\n f[0][0] = true;\n for (int i = 0; i <= m; i++) {\n for (int j = 1; j <= n; j++) {\n if (p[j-1] == '*') {\n f[i][j] = f[i][j-2];\n \n if (matches(s,p,i,j-1)) f[i][j] = f[i][j] || f[i-1][j];\n } else {\n if (matches(s,p,i,j)) f[i][j] = f[i-1][j-1];\n }\n }\n }\n return f[m][n];\n }\n\n bool matches(string& s, string p, int i, int j) {\n if (i == 0) return false;\n if (p[j-1] == '.') return true;\n return s[i-1] == p[j-1];\n }\n};",
"memory": "13681"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(const std::string& text, const std::string& pattern) {\n if (pattern.empty()) \n return text.empty();\n\n bool first_match = (!text.empty() &&\n (pattern[0] == text[0] || pattern[0] == '.'));\n\n if (pattern.length() >= 2 && pattern[1] == '*') {\n return (isMatch(text, pattern.substr(2)) ||\n (first_match && isMatch(text.substr(1), pattern)));\n } else {\n return (first_match && isMatch(text.substr(1), pattern.substr(1)));\n }\n }\n};\n",
"memory": "13868"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool helper(string s , string p, vector<vector<int>>&dp){\n if (p.length() == 0 && s.length() == 0) {\n return true;\n }\n if (s.length() == 0) {\n if (p.length() >= 2 && p[p.length() - 1] == '*' && helper(s, p.substr(0, p.length() - 2), dp)) {\n return true;\n }\n return false;\n }\n if (p.length() == 0) {\n return false;\n }\n if(dp[s.length()][p.length()]!=-1){\n return dp[s.length()][p.length()];\n }\n bool flag = false;\n if(s[s.length()-1] == p[p.length()-1]){\n flag = helper(s.substr(0,s.length()-1), p.substr(0,p.length()-1),dp);\n }else{\n if(p[p.length()-1] == '.'){\n flag = helper(s.substr(0,s.length()-1), p.substr(0,p.length()-1),dp);\n }else if(p[p.length()-1] == '*'){\n if(p.length() == 1){\n flag = flag || helper(s,p.substr(0,p.length()-1),dp);\n }else{\n if(p[p.length()-2] == s[s.length()-1] || p[p.length()-2] == '.'){\n flag = flag || helper(s.substr(0,s.length()-1), p,dp);\n flag = flag || helper(s,p.substr(0,p.length()-2),dp);\n }else{\n flag = flag || helper(s,p.substr(0,p.length()-2),dp);\n }\n }\n \n }\n }\n return dp[s.length()][p.length()] = flag;\n\n }\n bool isMatch(string s, string p) {\n vector<vector<int>>dp(s.length()+1,vector<int>(p.length()+s.length()+2,-1));\n return helper(s,p,dp);\n }\n};",
"memory": "13868"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n /* top down DP-approach */\n int s_len = s.length();\n int p_len = p.length();\n if (p_len == 0){\n return !s_len;\n }\n if (p[p_len-1] == '*'){\n if (s_len == 0){\n return isMatch(s,p.substr(0,p_len-2));\n }\n else if (s[s_len-1] == p[p_len-2] || p[p_len-2] == '.'){\n return isMatch(s.substr(0,s_len-1),p.substr(0,p_len-2)) || isMatch(s.substr(0,s_len-1), p.substr(0,p_len)) || isMatch(s, p.substr(0,p_len-2));\n }\n else{\n return isMatch(s, p.substr(0,p_len-2));\n }\n }\n else{\n if (s_len == 0){\n return false;\n }\n else if (s[s_len-1] == p[p_len-1] || p[p_len-1] == '.') {\n return isMatch(s.substr(0,s_len-1),p.substr(0,p_len-1));\n }\n else{\n return false;\n }\n }\n }\n};",
"memory": "14056"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool f(int i, int j, int star, string &s, string &p,vector<vector<vector<int>>> &dp)\n {\n if(i==-1)\n {\n if(j==-1)\n return true;\n else\n {\n if(j==0)\n return false;\n\n for(int k=0;k<j;k++)\n {\n if(p[k]!='*'&&p[k+1]!='*')\n {\n return false;\n }\n \n }\n if(p[j]!='*')\n return false;\n return true;\n }\n }\n if(j==-1)\n return false;\n\n if(dp[i][j][star]!=-1)\n return dp[i][j][star];\n if(!star)\n {\n if(p[j]=='.'||p[j]==s[i])\n return dp[i][j][star]=f(i-1,j-1,0,s,p,dp);\n\n if(p[j]=='*')\n return dp[i][j][star]=f(i,j-1,1,s,p,dp)|f(i,j-2,0,s,p,dp);\n\n return dp[i][j][star]=false;\n }\n else\n {\n if(p[j]=='.'||p[j]==s[i])\n return dp[i][j][star]=f(i-1,j-1,0,s,p,dp)|f(i-1,j,1,s,p,dp);\n if(p[j]!=s[i])\n return dp[i][j][star]=f(i,j-1,0,s,p,dp);\n return dp[i][j][star]=false;\n }\n\n }\n bool isMatch(string s, string p) {\n int n=s.size();\n int m=p.size();\n\n vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(m+1,vector<int>(2,-1)));\n return f(n-1,m-1,0,s,p,dp);\n }\n};",
"memory": "14056"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool match(string str, string pattern) {\n\n //std::cout << \"match(\" << str << \",\" << pattern << \")\"; << std::endl;\n\n if( str.empty() ) {\n if( pattern.empty() ) {\n return true;\n }\n\n for( char c : pattern ) {\n if( (c >= 'a' && c <= 'z') || c == '.' ) {\n return false;\n }\n }\n\n return true;\n }\n\n if( pattern.empty() ) {\n return false;\n }\n\n if( str.front() == pattern.front() || pattern.front() == '.' ) {\n return match(str.substr(1), pattern.substr(1));\n }\n\n bool frontStar = (pattern.front() >= 'A' && pattern.front() <= 'Z') || pattern.front() == '@';\n bool frontMatch = str.front() == (pattern.front() - ('A' - 'a')) || pattern.front() == '@';\n\n if( !frontStar ) {\n return false;\n }\n\n if( match(str, pattern.substr(1)) ) {\n return true;\n }\n else if( frontMatch ) {\n return match(str.substr(1), pattern.substr(1)) || match(str.substr(1), pattern);\n }\n\n return false;\n }\n\n bool isMatch(string s, string p) {\n size_t spos = 0;\n size_t ppos = 0;\n\n string pattern;\n for(size_t i =0; i < p.size(); i++) {\n if( i < p.size() -1 && p[i+1] == '*' ) {\n if( p[i] == '.' ) {\n if( pattern.empty() || pattern.back() != '@') {\n pattern.push_back('@');\n }\n }\n else {\n char symbol = p[i] + ('A' - 'a');\n\n if( pattern.empty() || pattern.back() != symbol) {\n pattern.push_back(symbol);\n }\n }\n\n i++;\n }\n else {\n pattern.push_back(p[i]);\n }\n }\n\n return match(s, pattern);\n }\n};",
"memory": "14243"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n \n if(!s.size() && !p.size()) {\n return true;\n }\n if(!p.size()) {\n return false;\n }\n \n if(p[1] == '*') {\n if(isMatch(s, p.substr(2))) {\n return true;\n }\n int i = 0;\n while(i < s.size() && (p[0] == s[i] || p[0] == '.' )) {\n i++;\n if(isMatch(s.substr(i), p.substr(2))){\n return true;\n }\n }\n return false;\n }\n else {\n if(s[0] == p[0] || (p[0] == '.' && s.size()))\n return isMatch(s.substr(1), p.substr(1));\n return false;\n }\n return false;\n }\n};",
"memory": "14243"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n bool isMatchHelper(string tab,string &s, string &p, int sp, int pp){\n bool retVal=false;\n \n //cout << tab << \"..........\" << endl;\n \n while(sp<s.size() && pp<p.size()){\n \n //cout << tab << \"s=\" << s.substr(sp) << endl;\n //cout << tab << \"p=\" << p.substr(pp) << endl;\n \n if(pp+1<p.size() && p[pp+1]=='*'){\n \n bool nextPart=false;\n \n //cout << tab << \"* handler, sp=\" << sp << \", pp=\" << pp << endl;\n //-------\n while(p[pp]==s[sp] || p[pp]=='.'){\n\n //cout << tab << \"sp=\" << sp << endl;\n\n //if(pp+2 >= p.size())\n // break;\n \n if(pp+4<p.size() && p[pp]==p[pp+2] && p[pp+1]==p[pp+3])\n break;\n \n if(pp+2 < p.size() && isMatchHelper(tab+\" \",s, p, sp, pp+2)){\n nextPart=true;\n break;\n }\n else{\n sp++;\n if(sp>=s.size())\n break;\n }\n }\n //----------\n \n if(!nextPart){\n nextPart = isMatchHelper(tab+\" \", s, p, sp, pp+2);\n //cout << tab << \"after * part returned : \" << (nextPart ? \"true\" : \"false\") << endl;\n }\n \n //cout << tab << \"*return \" << (nextPart ? \"true\" : \"false\") << endl;\n return nextPart;\n }\n \n if(s[sp]==p[pp] || p[pp]=='.'){\n sp++;\n pp++;\n }\n else{\n break;\n }\n }\n \n //cout << tab << \"sp=\" << sp << \", pp=\" << pp << endl;\n \n if(sp==s.size() && pp==p.size())\n retVal=true;\n \n //special case, no momre string, is the remaining p null?\n if(sp==s.size() && pp<p.size()){\n retVal=true;\n while(pp<p.size()){\n if(p[pp+1]!='*')\n retVal=false;\n pp+=2;\n }\n }\n \n //cout << tab << \"return \" << (retVal ? \"true\" : \"false\") << endl;\n return retVal;\n }\n \n bool isMatch(string s, string p) {\n int sp=0;\n int pp=0;\n return isMatchHelper(\"\", s, p, sp, pp);\n }\n};",
"memory": "14431"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\n typedef pair<int, char> Key;\n typedef set<int> Val;\n map<Key, Val> table;\n int accept;\npublic:\n // s = [a-z]* ; p = [a-z.*]* & valid (for * there is always a preceding character [a-z.])\n bool isMatch(string s, string p) {\n string q;\n const char *ptr = p.c_str();\n char chr;\n while (*ptr) {\n chr = *ptr++;\n if (chr >= 'a' && chr <= 'z' || chr == '.') {\n if (*ptr == '*') {\n ptr++;\n q.push_back(chr - 32);\n }\n else {\n q.push_back(chr);\n }\n }\n else {\n cout << \"Invalid character \" << chr << endl;\n return false;\n }\n }\n cout << \"Modified pattern = \" << q << endl;\n ptr = q.c_str();\n char last = *ptr;\n string r{ initializer_list<char>{ *ptr++ } };\n while (*ptr) {\n chr = *ptr++;\n if ((chr >= 'A' && chr <= 'Z' || chr == 14) && chr == last) {\n continue;\n }\n r.push_back(chr);\n last = chr;\n }\n cout << \"Shortened pattern = \" << r << endl;\n ptr = r.c_str();\n int cur = 0;\n while (*ptr) {\n chr = *ptr++;\n if (!(chr >= 'A' && chr <= 'Z' || chr >= 'a' && chr <= 'z' || chr == '.' || chr == 14)) {\n cout << \"Invalid character \" << chr << endl;\n return false;\n }\n if (chr >= 'A' && chr <= 'Z' || chr == 14) {\n update_set(Key{ cur, '\\0' }, cur + 1);\n update_set(Key{ cur + 1, chr + 32 }, cur + 1);\n }\n else {\n update_set(Key{ cur, chr }, cur + 1);\n }\n cur++;\n }\n accept = cur;\n\n for (const auto &[index, states] : table) {\n// if (index.second == '.') { continue; }\n cout << \"[\" << index.first << \";\" << index.second << \"] -> \";\n const char *sep = \"\";\n for (const auto &state : states) {\n cout << sep << state;\n sep = \" | \";\n }\n cout << endl;\n }\n\n for (const auto &[index, states] : table) {\n if (index.second == '.') {\n for (const auto &chr : string{ \"abcdefghijklmnopqrstuvwxyz\" } ) {\n for (const auto &state : states) {\n update_set(Key{ index.first, chr }, state);\n }\n }\n }\n }\n\n\n return match(s.c_str(), 0);\n }\n\n bool match(const char *input, int state) {\n cout << \"Trying \" << state << \" on '\" << input << \"'\" << endl;\n // empty transitions\n auto found_empty = table.find(make_pair(state, '\\0'));\n if (found_empty != table.cend()) {\n for (const auto &next : found_empty->second) {\n if (match(input, next)) {\n return true;\n }\n }\n }\n if (*input == '\\0') {\n return state == accept;\n }\n auto found = table.find(make_pair(state, *input));\n if (found == table.cend()) {\n return false;\n }\n input++;\n for (const auto &next : found->second) {\n if (match(input, next)) {\n return true;\n }\n }\n return false;\n }\n\n void update_set(const Key &idx, int state) {\n auto found = table.find(idx);\n if (found == table.cend()) {\n table[idx] = Val{ initializer_list<int>{ state } };\n }\n else {\n found->second.insert(state);\n }\n }\n};\n",
"memory": "15368"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "template<typename T>\nvoid\nhash_combine(std::size_t &seed, T const &key) {\n std::hash<T> hasher;\n seed ^= hasher(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n}\n\nnamespace std {\n template<typename T1, typename T2>\n struct hash<std::pair<T1, T2>> {\n std::size_t operator()(std::pair<T1, T2> const &p) const {\n std::size_t seed1(0);\n ::hash_combine(seed1, p.first);\n ::hash_combine(seed1, p.second);\n\n std::size_t seed2(0);\n ::hash_combine(seed2, p.second);\n ::hash_combine(seed2, p.first);\n\n return std::min(seed1, seed2);\n }\n };\n}\n\n\nclass Solution {\n // Means that s[0..strIdx-1] is matched with p[0..patIdx-1]\n struct MatchState {\n int strIdx;\n int patIdx;\n };\n\n struct PatternMatch {\n char c;\n bool repeat;\n };\n\npublic:\n bool isMatch(string s, string p) {\n std::unordered_set<std::pair<int, int>> states;\n states.emplace(0, 0);\n\n std::vector<PatternMatch> pattern;\n for (int i = 0; i < p.size(); i++) {\n if (p[i] == '*') continue; // handled in prev iteration of loop\n\n if (i + 1 < p.size() && p[i + 1] == '*') {\n pattern.emplace_back(p[i], true);\n } else {\n pattern.emplace_back(p[i], false);\n }\n }\n\n while (states.size() != 0) {\n decltype(states) newStates;\n\n for (const auto [strIdx, patIdx]: states) {\n if (patIdx == pattern.size()) {\n if (strIdx == s.size()) {\n return true;\n }\n continue;\n }\n\n // ASSERT : patIdx < pattern.size()\n if (pattern[patIdx].repeat) {\n newStates.emplace(strIdx, patIdx + 1);\n }\n\n if (strIdx == s.size()) {\n continue;\n }\n // ASSERT: strIdx < s.size() && patIdx < pattern.size()\n\n if (s[strIdx] == pattern[patIdx].c || pattern[patIdx].c == '.') {\n newStates.emplace(strIdx + 1, patIdx + 1);\n if (pattern[patIdx].repeat) {\n newStates.emplace(strIdx + 1, patIdx);\n }\n }\n }\n\n states = newStates;\n }\n\n return false;\n }\n};",
"memory": "15368"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n map<pair<int,int>, long long> dp;\n bool isMatchUtil(string &s, string &p , int n, int m){\n if(n == -1 && m == -1)\n return true;\n if(dp.find({n, m}) != dp.end())\n return dp[{n, m}];\n if(m == -1)\n return false;\n if(n == -1){\n if(p[m] != '*')\n return false;\n return dp[{n, m}]=isMatchUtil(s, p, n, m-2);\n }\n\n bool res = false;\n if(p[m] == '.'){\n res = isMatchUtil(s, p, n-1, m-1);\n }\n else if(p[m] == '*'){\n res = isMatchUtil(s, p, n, m-2);\n if(p[m-1] == '.' || s[n] == p[m-1])\n res = res || isMatchUtil(s, p, n-1, m);\n }\n else\n res = (s[n] == p[m]) && isMatchUtil(s, p, n-1, m-1);\n return dp[{n, m}] = res;\n }\n bool isMatch(string s, string p) {\n dp.clear();\n string regex = \"\";\n char l = '0';\n for(auto c: p){\n if(c!=l || c != '*'){\n l = c;\n regex += c;\n }\n }\n return isMatchUtil(s, regex, s.size() - 1, regex.size() - 1);\n }\n};",
"memory": "15556"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n try {\n // Create a regex object from the pattern, with the pattern being used directly\n regex pattern(p);\n // Use regex_match to check if the whole string matches the pattern\n return regex_match(s, pattern);\n } catch (const regex_error& e) {\n // Handle regex errors (e.g., invalid pattern)\n cerr << \"Regex error: \" << e.what() << '\\n';\n return false;\n }\n }\n};",
"memory": "15556"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "\n#include <regex>\n\nclass Solution {\npublic:\n bool isMatch(string s, string p) {\n regex r(p);\n return match_regex(s, r);\n }\n\nprivate:\n bool match_regex(const string& s, const regex& r) {\n return regex_match(s, r);\n }\n};",
"memory": "15743"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool isOK(const short sM, const short pM, short i, short n, string s, string p)\n {\n here:\n if (i>sM) \n {\n if (n>pM) return true;\n for (short x = n+1; x<pM+2 ; x+=2)\n {\n if (p[x]!='*') return false;\n }\n return true;\n\n }\n\n if (n>pM) return false;\n\n if (p[n+1]=='*')\n {\n n+=2;\n if (p[n]==p[n-2]&&p[n+1]=='*') { goto here;};\n \n while (s[i]==p[n-2]||p[n-2]=='.')\n {\n if (isOK(sM,pM,i,n,s,p)) return true;\n ++i;\n if (i>sM) goto here;\n } \n goto here;\n }\n \n \n if (s[i]==p[n]||p[n]=='.') if (isOK(sM,pM,i+1,n+1,s,p)) return true;\n \n return false;\n }\n\n bool isMatch(string s, string p)\n {\n if (isOK(s.length()-1,p.length()-1,0,0,s,p)) return true;\n else return false;\n }\n};",
"memory": "15743"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution\r\n{\r\npublic:\r\n bool isMatch(string s, string p)\r\n {\r\n return std::regex_match(s, std::regex(p));\r\n }\r\n};",
"memory": "15931"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n p.insert(0, \"^\");\n p.append(\"$\");\n return std::regex_match(s.data(), std::regex(p.data()));\n }\n};",
"memory": "15931"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "struct hasher {\n size_t operator()(pair<int, int> p) const noexcept {\n size_t h = 5381;\n h = (h * 33) + p.first;\n h = (h * 33) + p.second;\n return h;\n }\n};\n\nclass Solution {\npublic:\n unordered_map<pair<int, int>, bool, hasher> memo;\n\n bool isMatch(string s, string p) { return this->aux(s, p, 0, 0); }\n\n bool aux(const string& s, const string& p, int i, int j) {\n pair<int, int> q = make_pair(i, j);\n auto mitr = memo.find(q);\n if (mitr != memo.end()) {\n return mitr->second;\n }\n if (j == p.size()) {\n // note - if pattern not ended\n // but there is string left, it's still possible to succeed\n // becaues of star - for example \"\" matches re \"a*\"\n // but if pattern ended and there is string left then we fail\n return (i == s.size());\n }\n\n // does first character match?\n bool first_match = (i < s.size()) && (p[j] == '.' || p[j] == s[i]);\n\n // is there a star?\n if (j < p.size() - 1 && p[j + 1] == '*') {\n // option 1 - skip\n bool match1 = this->aux(s, p, i, j + 2);\n if (match1) {\n // cout << \" zero * char match\\n\";\n return true;\n }\n // option 2 - match first token (already done) and recurse\n bool out = first_match && this->aux(s, p, i + 1, j);\n memo[make_pair(i, j)] = out;\n return out;\n } else {\n // cout << \"one char match\\n\";\n // if no star, keep going with simple match\n bool out = first_match && this->aux(s, p, i + 1, j + 1);\n memo[make_pair(i, j)] = out;\n return out;\n }\n return true;\n }\n};",
"memory": "16118"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "#include <map>\n\nstruct indices {\n indices(int ii, int jj) : i(ii), j(jj) {}\n int i;\n int j;\n};\nbool operator<(const indices& l, const indices& r) {\n return (l.i < r.i || (l.i == r.i && l.j < r.j));\n}\n\nclass Solution {\n\nprivate:\n string str;\n string pat;\n\n int sl;\n int pl;\n\n std::map<indices, bool> memo{};\n\n bool r(int i, int j) {\n indices ind(i, j);\n if (memo.contains(ind))\n return memo[ind];\n\n if (i >= sl && j >= pl)\n return true;\n if (j >= pl)\n return false;\n\n bool match = i < sl && (str[i] == pat[j] || pat[j] == '.');\n\n if (j + 1 < pl && pat[j + 1] == '*') {\n memo[ind] = r(i, j + 2) || (match && r(i + 1, j));\n return memo[ind];\n } else if (match) {\n memo[ind] = r(i + 1, j + 1);\n return memo[ind];\n } else {\n memo[ind] = false;\n return memo[ind];\n }\n }\n\npublic:\n bool isMatch(string s, string p) {\n str = s;\n pat = p;\n sl = str.length();\n pl = pat.length();\n return r(0, 0);\n }\n};",
"memory": "16118"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool help(string& s,string& p,int i,int j,vector<unordered_map<int,bool>>& hash)\n {\n \n if((i==s.length())&&(j==p.length()))\n {\n return true;\n }\n else if(j==p.length())\n {\n return false;\n }\n else if(i==s.length())\n {\n for(;j<p.length();j=j+2)\n {\n if(!((j+1<p.length())&&(p[j+1]=='*')))\n {\n return false;\n }\n }\n return true;\n }\n if(hash[i].count(j))\n {\n return hash[i][j];\n }\n if((j+1<p.length())&&(p[j+1]=='*'))\n {\n bool dis=help(s,p,i,j+2,hash);\n bool con=false;\n if((s[i]==p[j])||(p[j]=='.'))\n {\n con=help(s,p,i+1,j,hash);\n }\n hash[i][j]=dis||con;\n return dis||con;\n }\n else\n {\n bool result=false;\n if((s[i]==p[j])||(p[j]=='.'))\n {\n result=help(s,p,i+1,j+1,hash);\n }\n hash[i][j]=result;\n return result;\n }\n }\n bool isMatch(string s, string p) \n {\n int m=s.length(),n=p.length();\n vector<unordered_map<int,bool>> hash(m);\n return help(s,p,0,0,hash);\n }\n};",
"memory": "16306"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "template <>\nstruct std::hash<std::pair<size_t, size_t>>{\n size_t operator()(const std::pair<size_t, size_t>& p) const noexcept {\n const auto hash1 = std::hash<size_t>{}(p.first);\n const auto hash2 = std::hash<size_t>{}(p.second);\n return hash1 ^ hash2 << 1;\n }\n};\n\nclass Solution {\npublic:\n bool isMatch(const std::string& s, const std::string& p) {\n std::unordered_map<std::pair<size_t, size_t>, bool> cache;\n std::function<bool(size_t i, size_t j)> dfs = [&](size_t i, size_t j) {\n if (cache.contains({i, j})) {\n return cache[{i, j}];\n }\n if (i >= s.size() && j >= p.size()) {\n return true;\n }\n if (j >= p.size()) {\n return false;\n }\n const bool are_matched = i < s.size()\n && (s[i] == p[j] || p[j] == '.');\n if (j + 1 < p.size() && p[j + 1] == '*') {\n cache[{i, j}] = dfs(i, j + 2) || are_matched && dfs(i + 1, j);\n return cache[{i, j}];\n }\n if (are_matched) {\n cache[{i, j}] = dfs(i + 1, j + 1);\n return cache[{i, j}];\n }\n return cache[{i, j}] = false;\n return cache[{i, j}];\n };\n return dfs(0, 0);\n }\n};",
"memory": "16493"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "template <>\nstruct std::hash<std::pair<size_t, size_t>>{\n size_t operator()(const std::pair<size_t, size_t>& p) const noexcept {\n const auto hash1 = std::hash<size_t>{}(p.first);\n const auto hash2 = std::hash<size_t>{}(p.second);\n return hash1 ^ hash2 << 1;\n }\n};\n\nclass Solution {\npublic:\n bool isMatch(const std::string& s, const std::string& p) {\n std::unordered_map<std::pair<size_t, size_t>, bool> cache;\n std::function<bool(size_t i, size_t j)> dfs = [&](size_t i, size_t j) {\n if (cache.contains({i, j})) {\n return cache[{i, j}];\n }\n if (i >= s.size() && j >= p.size()) {\n return true;\n }\n if (j >= p.size()) {\n return false;\n }\n const bool are_matched = i < s.size()\n && (s[i] == p[j] || p[j] == '.');\n if (j + 1 < p.size() && p[j + 1] == '*') {\n cache[{i, j}] = dfs(i, j + 2) || are_matched && dfs(i + 1, j);\n return cache[{i, j}];\n }\n if (are_matched) {\n cache[{i, j}] = dfs(i + 1, j + 1);\n return cache[{i, j}];\n }\n return cache[{i, j}] = false;\n return cache[{i, j}];\n };\n return dfs(0, 0);\n }\n};",
"memory": "16493"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n unordered_map<pair<int, int>, bool, hash_pair> cache; // Top-Down Memoization\n \n function<bool(int, int)> dfs = [&](int i, int j) -> bool {\n if (cache.find({i, j}) != cache.end()) return cache[{i, j}];\n if (i >= s.size() && j >= p.size()) return true;\n if (j >= p.size()) return false;\n \n bool match = i < s.size() && (s[i] == p[j] || p[j] == '.');\n \n if ((j + 1) < p.size() && p[j + 1] == '*') {\n cache[{i, j}] = dfs(i, j + 2) || (match && dfs(i + 1, j));\n return cache[{i, j}];\n }\n \n if (match) {\n cache[{i, j}] = dfs(i + 1, j + 1);\n return cache[{i, j}];\n }\n \n cache[{i, j}] = false;\n return false;\n };\n \n return dfs(0, 0);\n }\n\nprivate:\n // Custom hash function for pair<int, int>\n struct hash_pair {\n template <class T1, class T2>\n size_t operator()(const pair<T1, T2>& p) const {\n auto hash1 = hash<T1>{}(p.first);\n auto hash2 = hash<T2>{}(p.second);\n return hash1 ^ hash2;\n }\n };\n};",
"memory": "16681"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n return dfs(s, p, 0, 0);\n }\nprivate:\n map<pair<int,int>, int> mp;\n\n bool dfs(string& s, string& p, int i, int j){\n if(mp.find({i, j})!=mp.end())\n return mp[{i, j}];\n\n if(i>=s.size() && j>=p.size())\n return true;\n if(j>=p.size())\n return false;\n \n bool match = i<s.size() && (s[i]==p[j] || p[j]=='.');\n\n if(j+1<p.size() && p[j+1]=='*')\n mp[{i, j}] = (match && dfs(s, p, i+1,j)) || dfs(s, p, i, j+2);\n else if(match)\n mp[{i, j}] = dfs(s, p, i+1, j+1);\n else\n mp[{i, j}] = false;\n \n return mp[{i, j}];\n }\n};",
"memory": "16681"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "/*\n Given string & pattern, implement RegEx matching\n '.' -> matches any single character\n '*' -> matches zero or more of the preceding element\n Matching should cover the entire input string (not partial)\n Ex. s = \"aa\", p = \"a\" -> false, \"a\" doesn't match entire string \"aa\"\n\n DFS + memo, 2 choices at a *: either use it, or don't use it\n\n Time: O(m x n)\n Space: O(m x n)\n*/\n\nclass Solution {\npublic:\n bool isMatch(string s, string p) {\n return dfs(s, p, 0, 0);\n }\nprivate:\n map<pair<int, int>, bool> dp;\n \n bool dfs(string& s, string& p, int i, int j) {\n if (dp.find({i, j}) != dp.end()) {\n return dp[{i, j}];\n }\n \n if (i >= s.size() && j >= p.size()) {\n return true;\n }\n if (j >= p.size()) {\n return false;\n }\n \n bool match = i < s.size() && (s[i] == p[j] || p[j] == '.');\n if (j + 1 < p.size() && p[j + 1] == '*') {\n // choices: either (1) don't use *, or (2) use *\n dp[{i, j}] = dfs(s, p, i, j + 2) || (match && dfs(s, p, i + 1, j));\n return dp[{i, j}];\n }\n \n if (match) {\n dp[{i, j}] = dfs(s, p, i + 1, j + 1);\n return dp[{i, j}];\n }\n \n dp[{i, j}] = false;\n return dp[{i, j}];\n }\n};\n",
"memory": "16868"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "/*\n Given string & pattern, implement RegEx matching\n '.' -> matches any single character\n '*' -> matches zero or more of the preceding element\n Matching should cover the entire input string (not partial)\n Ex. s = \"aa\", p = \"a\" -> false, \"a\" doesn't match entire string \"aa\"\n\n DFS + memo, 2 choices at a *: either use it, or don't use it\n\n Time: O(m x n)\n Space: O(m x n)\n*/\n\nclass Solution {\npublic:\n bool isMatch(string s, string p) {\n return dfs(s, p, 0, 0);\n }\nprivate:\n map<pair<int, int>, bool> dp;\n \n bool dfs(string& s, string& p, int i, int j) {\n if (dp.find({i, j}) != dp.end()) {\n return dp[{i, j}];\n }\n \n if (i >= s.size() && j >= p.size()) {\n return true;\n }\n if (j >= p.size()) {\n return false;\n }\n \n bool match = i < s.size() && (s[i] == p[j] || p[j] == '.');\n if (j + 1 < p.size() && p[j + 1] == '*') {\n // choices: either (1) don't use *, or (2) use *\n dp[{i, j}] = dfs(s, p, i, j + 2) || (match && dfs(s, p, i + 1, j));\n return dp[{i, j}];\n }\n \n if (match) {\n dp[{i, j}] = dfs(s, p, i + 1, j + 1);\n return dp[{i, j}];\n }\n \n dp[{i, j}] = false;\n return dp[{i, j}];\n }\n};\n",
"memory": "16868"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(string s, string p) {\n return dfs(s, p, 0, 0);\n }\nprivate:\n map<pair<int, int>, bool> dp;\n \n bool dfs(string& s, string& p, int i, int j) {\n if (dp.find({i, j}) != dp.end()) {\n return dp[{i, j}];\n }\n \n if (i >= s.size() && j >= p.size()) {\n return true;\n }\n if (j >= p.size()) {\n return false;\n }\n \n bool match = i < s.size() && (s[i] == p[j] || p[j] == '.');\n if (j + 1 < p.size() && p[j + 1] == '*') {\n // choices: either (1) don't use *, or (2) use *\n dp[{i, j}] = dfs(s, p, i, j + 2) || (match && dfs(s, p, i + 1, j));\n return dp[{i, j}];\n }\n \n if (match) {\n dp[{i, j}] = dfs(s, p, i + 1, j + 1);\n return dp[{i, j}];\n }\n \n dp[{i, j}] = false;\n return dp[{i, j}];\n }\n};",
"memory": "17056"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(const string& s, string p) {\n bool isMatch = false;\n p = regex_replace(p, regex(R\"(\\*+)\"), \"*\");\n p = \"^\" + p + \"$\";\n regex regexPattern(p);\n if(regex_match(s, regexPattern)) {\n isMatch = true;\n }\n\n return isMatch;\n }\n};",
"memory": "17056"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(const string& s, string p) {\n bool isMatch = false;\n p = regex_replace(p, regex(R\"(\\*+)\"), \"*\");\n p = \"^\" + p + \"$\";\n regex regexPattern(p);\n if(regex_match(s, regexPattern)) {\n isMatch = true;\n }\n\n return isMatch;\n }\n};",
"memory": "17243"
} |
10 | <p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
<ul>
<li><code>'.'</code> Matches any single character.</li>
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
</ul>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aa", p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "ab", p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>1 <= p.length <= 20</code></li>
<li><code>s</code> contains only lowercase English letters.</li>
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isMatch(const string& s, string p) {\n bool isMatch = false;\n p = regex_replace(p, regex(R\"(\\*+)\"), \"*\");\n p = \"^\" + p + \"$\";\n regex regexPattern(p);\n if(regex_match(s, regexPattern)) {\n isMatch = true;\n }\n\n return isMatch;\n }\n};\n \n \n \n \n \n \n \n \n \n\n \n \n \n \n\n\n \n\n \n \n \n \n\n\n\n\n\n\n\n\n\n \n\n \n\n \n \n \n\n\n\n \n \n \n \n\n \n \n \n\n\n \n \n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n \n \n \n \n\n\n\n\n \n\n \n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n \n \n",
"memory": "17243"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.