1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 10:38:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
程序员小墨 2023-09-09 15:37:57 +08:00
parent 9eb643f137
commit d2fdf8e86f
47 changed files with 18207 additions and 13501 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,59 @@
{
"data": {
"question": {
"questionId": "2821",
"questionFrontendId": "2715",
"categoryTitle": "JavaScript",
"boundTopicId": 2293968,
"title": "Timeout Cancellation",
"titleSlug": "timeout-cancellation",
"content": "<p>Given a function <code>fn</code>, an array of&nbsp;arguments&nbsp;<code>args</code>, and a timeout&nbsp;<code>t</code>&nbsp;in milliseconds, return a cancel function <code>cancelFn</code>.</p>\n\n<p>After a delay of&nbsp;<code>t</code>,&nbsp;<code>fn</code>&nbsp;should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was invoked before the delay of <code>t</code> milliseconds elapses, specifically at <code>cancelT</code>&nbsp;ms.&nbsp;In that case,&nbsp;<code>fn</code> should never be called.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20, cancelT = 50\n<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]\n<strong>Explanation:</strong> \nconst cancel = cancellable((x) =&gt; x * 5, [2], 20); // fn(2) called at t=20ms\nsetTimeout(cancel, 50);\n\nThe cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100, cancelT = 50 \n<strong>Output:</strong> []\n<strong>Explanation:</strong> \nconst cancel = cancellable((x) =&gt; x**2, [2], 100); // fn(2) not called\nsetTimeout(cancel, 50);\n\nThe cancellation was scheduled to occur after a delay of cancelT (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelT = 100\n<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]\n<strong>Explanation:</strong>\nconst cancel = cancellable((x1, x2) =&gt; x1 * x2, [2,4], 30); // fn(2,4) called at t=30ms\nsetTimeout(cancel, 100);\n\nThe cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>fn is a function</code></li>\n\t<li><code>args is a valid JSON array</code></li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">20 &lt;= t &lt;= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= cancelT &lt;= 1000</font></code></li>\n</ul>\n",
"translatedTitle": "执行可取消的延迟函数",
"translatedContent": "<p>现给定一个函数 <code>fn</code>&nbsp;,一个参数数组 <code>args</code> 和一个以毫秒为单位的超时时间 <code>t</code> ,返回一个取消函数 <code>cancelFn</code> 。</p>\n\n<p>在经过 <code>t</code> 毫秒的延迟后,应该调用 <code>fn</code> 函数,并将 <code>args</code> 作为参数传递。<strong>除非</strong> 在 <code>t</code> 毫秒的延迟过程中,在 <code>cancelT</code> 毫秒时调用了 <code>cancelFn</code>。并且在这种情况下,<code>fn</code> 函数不应该被调用。</p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x) =&gt; x * 5, args = [2], t = 20, cancelT = 50\n<b>输出:</b>[{\"time\": 20, \"returned\": 10}]\n<b>解释:</b>\nconst cancel = cancellable((x) =&gt; x * 5, [2], 20); // fn(2) 在 t=20ms 时被调用\nsetTimeout(cancel, 50);\n\n取消操作被安排在延迟了 cancelT50毫秒后进行这发生在 fn(2) 在20毫秒时执行之后。</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x) =&gt; x**2, args = [2], t = 100, cancelT = 50\n<b>输出:</b>[]\n<b>解释:</b>\nconst cancel = cancellable((x) =&gt; x**2, [2], 100); // fn(2) 没被调用\nsetTimeout(cancel, 50);\n\n取消操作被安排在延迟了 cancelT50毫秒后进行这发生在 fn(2) 在100毫秒时执行之前导致 fn(2) 从未被调用。\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelTime = 100\n<b>输出:</b>[{\"time\": 30, \"returned\": 8}]\n<b>解释:</b>\nconst cancel = cancellable((x1, x2) =&gt; x1 * x2, [2,4], 30); // fn(2,4) 在 t=30ms 时被调用\nsetTimeout(cancel, 100);\n\n取消操作被安排在延迟了 cancelT100毫秒后进行这发生在 fn(2,4) 在30毫秒时执行之后。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>fn 是一个函数</code></li>\n\t<li><code>args 是一个有效的 JSON 数组</code></li>\n\t<li><code>1 &lt;= args.length &lt;= 10</code></li>\n\t<li><code><font face=\"monospace\">20 &lt;= t &lt;= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 &lt;= cancelT &lt;= 1000</font></code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 2,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Function} fn\n * @param {Array} args\n * @param {number} t\n * @return {Function}\n */\nvar cancellable = function(fn, args, t) {\n \n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const start = performance.now() \n *\n * const log = (...argsArr) => {\n * const diff = Math.floor(performance.now() - start);\n * result.push({\"time\": diff, \"returned\": fn(...argsArr)})\n * }\n * \n * const cancel = cancellable(log, args, t);\n *\n * const maxT = Math.max(t, cancelT)\n * \n * setTimeout(() => {\n * cancel()\n * }, cancelT)\n *\n * setTimeout(() => {\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, maxT + 15)\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function cancellable(fn: Function, args: any[], t: number): Function {\n\n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const start = performance.now() \n *\n * const log = (...argsArr) => {\n * const diff = Math.floor(performance.now() - start);\n * result.push({\"time\": diff, \"returned\": fn(...argsArr)})\n * }\n * \n * const cancel = cancellable(log, args, t);\n *\n * const maxT = Math.max(t, cancelT)\n * \n * setTimeout(() => {\n * cancel()\n * }, cancelT)\n *\n * setTimeout(() => {\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, maxT + 15)\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"1.9K\", \"totalSubmission\": \"2.4K\", \"totalAcceptedRaw\": 1914, \"totalSubmissionRaw\": 2430, \"acRate\": \"78.8%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "(x) => x * 5\n[2]\n20\n50",
"metaData": "{\n \"name\": \"cancellable\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"cancelT\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "(x) => x * 5\n[2]\n20\n50\n(x) => x**2\n[2]\n100\n50\n(x1, x2) => x1 * x2\n[2,4]\n30\n100",
"__typename": "QuestionNode"
}
}
}

View File

@ -0,0 +1,43 @@
<p>给你一个整数数组&nbsp;<code>nums</code>&nbsp;和两个正整数&nbsp;<code>m</code>&nbsp;&nbsp;<code>k</code>&nbsp;</p>
<p>请你返回 <code>nums</code>&nbsp;中长度为 <code>k</code>&nbsp;&nbsp;<strong>几乎唯一</strong>&nbsp;子数组的 <strong>最大和</strong>&nbsp;,如果不存在几乎唯一子数组,请你返回 <code>0</code>&nbsp;</p>
<p>如果 <code>nums</code>&nbsp;的一个子数组有至少 <code>m</code>&nbsp;个互不相同的元素,我们称它是 <strong>几乎唯一</strong>&nbsp;子数组。</p>
<p>子数组指的是一个数组中一段连续 <strong>非空</strong>&nbsp;的元素序列。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>nums = [2,6,7,3,1,7], m = 3, k = 4
<b>输出:</b>18
<b>解释:</b>总共有 3 个长度为 k = 4 的几乎唯一子数组。分别为 [2, 6, 7, 3] [6, 7, 3, 1] 和 [7, 3, 1, 7] 。这些子数组中,和最大的是 [2, 6, 7, 3] ,和为 18 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>nums = [5,9,9,2,4,5,4], m = 1, k = 3
<b>输出:</b>23
<b>解释:</b>总共有 5 个长度为 k = 3 的几乎唯一子数组。分别为 [5, 9, 9] [9, 9, 2] [9, 2, 4] [2, 4, 5] 和 [4, 5, 4] 。这些子数组中,和最大的是 [5, 9, 9] ,和为 23 。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>nums = [1,2,1,2,1,2,1], m = 3, k = 3
<b>输出:</b>0
<b>解释:</b>输入数组中不存在长度为 <code>k = 3</code> 的子数组含有至少 <code>m = 3</code> 个互不相同元素的子数组。所以不存在几乎唯一子数组,最大和为 0 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= m &lt;= k &lt;= nums.length</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@ -0,0 +1,38 @@
<p>给你两个字符串&nbsp;<code>s1</code>&nbsp;<code>s2</code>&nbsp;,两个字符串的长度都为&nbsp;<code>4</code>&nbsp;,且只包含 <strong>小写</strong> 英文字母。</p>
<p>你可以对两个字符串中的 <strong>任意一个</strong>&nbsp;执行以下操作 <strong>任意</strong>&nbsp;次:</p>
<ul>
<li>选择两个下标&nbsp;<code>i</code>&nbsp;<code>j</code>&nbsp;且满足&nbsp;<code>j - i = 2</code>&nbsp;,然后 <strong>交换</strong> 这个字符串中两个下标对应的字符。</li>
</ul>
<p>如果你可以让字符串<em>&nbsp;</em><code>s1</code><em> </em><em>&nbsp;</em><code>s2</code>&nbsp;相等,那么返回 <code>true</code>&nbsp;,否则返回 <code>false</code>&nbsp;</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>s1 = "abcd", s2 = "cdab"
<b>输出:</b>true
<strong>解释:</strong> 我们可以对 s1 执行以下操作:
- 选择下标 i = 0 j = 2 ,得到字符串 s1 = "cbad" 。
- 选择下标 i = 1 j = 3 ,得到字符串 s1 = "cdab" = s2 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>s1 = "abcd", s2 = "dacb"
<b>输出:</b>false
<b>解释:</b>无法让两个字符串相等。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>s1.length == s2.length == 4</code></li>
<li><code>s1</code>&nbsp;<code>s2</code>&nbsp;只包含小写英文字母。</li>
</ul>

View File

@ -0,0 +1,44 @@
<p>给你两个字符串&nbsp;<code>s1</code>&nbsp;&nbsp;<code>s2</code>&nbsp;,两个字符串长度都为&nbsp;<code>n</code>&nbsp;,且只包含&nbsp;<strong>小写&nbsp;</strong>英文字母。</p>
<p>你可以对两个字符串中的 <strong>任意一个</strong>&nbsp;执行以下操作 <strong>任意</strong>&nbsp;次:</p>
<ul>
<li>选择两个下标&nbsp;<code>i</code>&nbsp;<code>j</code>&nbsp;,满足 <code>i &lt; j</code>&nbsp;<code>j - i</code>&nbsp;<strong>偶数</strong>,然后 <strong>交换</strong> 这个字符串中两个下标对应的字符。</li>
</ul>
<p>&nbsp;</p>
<p>如果你可以让字符串<em>&nbsp;</em><code>s1</code><em> </em><em>&nbsp;</em><code>s2</code>&nbsp;相等,那么返回 <code>true</code>&nbsp;,否则返回 <code>false</code>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>s1 = "abcdba", s2 = "cabdab"
<b>输出:</b>true
<b>解释:</b>我们可以对 s1 执行以下操作:
- 选择下标 i = 0 j = 2 ,得到字符串 s1 = "cbadba" 。
- 选择下标 i = 2 j = 4 ,得到字符串 s1 = "cbbdaa" 。
- 选择下标 i = 1 j = 5 ,得到字符串 s1 = "cabdab" = s2 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>s1 = "abe", s2 = "bea"
<b>输出:</b>false
<b>解释:</b>无法让两个字符串相等。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == s1.length == s2.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>s1</code>&nbsp;<code>s2</code>&nbsp;只包含小写英文字母。</li>
</ul>

View File

@ -0,0 +1,50 @@
<p>现给定一个函数 <code>fn</code>&nbsp;,一个参数数组 <code>args</code> 和一个以毫秒为单位的超时时间 <code>t</code> ,返回一个取消函数 <code>cancelFn</code></p>
<p>在经过 <code>t</code> 毫秒的延迟后,应该调用 <code>fn</code> 函数,并将 <code>args</code> 作为参数传递。<strong>除非</strong><code>t</code> 毫秒的延迟过程中,在 <code>cancelT</code> 毫秒时调用了 <code>cancelFn</code>。并且在这种情况下,<code>fn</code> 函数不应该被调用。</p>
<p><strong class="example">示例 1:</strong></p>
<pre>
<b>输入:</b>fn = (x) =&gt; x * 5, args = [2], t = 20, cancelT = 50
<b>输出:</b>[{"time": 20, "returned": 10}]
<b>解释:</b>
const cancel = cancellable((x) =&gt; x * 5, [2], 20); // fn(2) 在 t=20ms 时被调用
setTimeout(cancel, 50);
取消操作被安排在延迟了 cancelT50毫秒后进行这发生在 fn(2) 在20毫秒时执行之后。</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>fn = (x) =&gt; x**2, args = [2], t = 100, cancelT = 50
<b>输出:</b>[]
<b>解释:</b>
const cancel = cancellable((x) =&gt; x**2, [2], 100); // fn(2) 没被调用
setTimeout(cancel, 50);
取消操作被安排在延迟了 cancelT50毫秒后进行这发生在 fn(2) 在100毫秒时执行之前导致 fn(2) 从未被调用。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelTime = 100
<b>输出:</b>[{"time": 30, "returned": 8}]
<b>解释:</b>
const cancel = cancellable((x1, x2) =&gt; x1 * x2, [2,4], 30); // fn(2,4) 在 t=30ms 时被调用
setTimeout(cancel, 100);
取消操作被安排在延迟了 cancelT100毫秒后进行这发生在 fn(2,4) 在30毫秒时执行之后。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>fn 是一个函数</code></li>
<li><code>args 是一个有效的 JSON 数组</code></li>
<li><code>1 &lt;= args.length &lt;= 10</code></li>
<li><code><font face="monospace">20 &lt;= t &lt;= 1000</font></code></li>
<li><code><font face="monospace">10 &lt;= cancelT &lt;= 1000</font></code></li>
</ul>

View File

@ -0,0 +1,46 @@
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>num</code> ,表示一个非负整数。</p>
<p>在一次操作中,您可以选择 <code>num</code> 的任意一位数字并将其删除。请注意,如果你删除 <code>num</code> 中的所有数字,则 <code>num</code> 变为 <code>0</code></p>
<p>返回最少需要多少次操作可以使 <code>num</code> 变成特殊数字。</p>
<p>如果整数 <code>x</code> 能被 <code>25</code> 整除,则该整数 <code>x</code> 被认为是特殊数字。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>num = "2245047"
<strong>输出:</strong>2
<strong>解释:</strong>删除数字 num[5] 和 num[6] ,得到数字 "22450" ,可以被 25 整除。
可以证明要使数字变成特殊数字,最少需要删除 2 位数字。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>num = "2908305"
<strong>输出:</strong>3
<strong>解释:</strong>删除 num[3]、num[4] 和 num[6] ,得到数字 "2900" ,可以被 25 整除。
可以证明要使数字变成特殊数字,最少需要删除 3 位数字。</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>num = "10"
<strong>输出:</strong>1
<strong>解释:</strong>删除 num[0] ,得到数字 "0" ,可以被 25 整除。
可以证明要使数字变成特殊数字,最少需要删除 1 位数字。
</pre>
<p>&nbsp;</p>
<p><strong>提示</strong></p>
<ul>
<li><code>1 &lt;= num.length &lt;= 100</code></li>
<li><code>num</code> 仅由数字 <code>'0'</code><code>'9'</code> 组成</li>
<li><code>num</code> 不含任何前导零</li>
</ul>

View File

@ -0,0 +1,72 @@
<p>给你一个字符串&nbsp;<code>s</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;</p>
<p><strong>k 子序列</strong>指的是 <code>s</code>&nbsp;的一个长度为 <code>k</code>&nbsp;<strong>子序列</strong>&nbsp;,且所有字符都是 <strong>唯一</strong>&nbsp;的,也就是说每个字符在子序列里只出现过一次。</p>
<p>定义&nbsp;<code>f(c)</code>&nbsp;为字符 <code>c</code>&nbsp;<code>s</code>&nbsp;中出现的次数。</p>
<p>k 子序列的 <strong>美丽值</strong>&nbsp;定义为这个子序列中每一个字符 <code>c</code>&nbsp;&nbsp;<code>f(c)</code>&nbsp;<strong></strong>&nbsp;</p>
<p>比方说,<code>s = "abbbdd"</code>&nbsp;&nbsp;<code>k = 2</code>&nbsp;,我们有:</p>
<ul>
<li><code>f('a') = 1</code>, <code>f('b') = 3</code>, <code>f('d') = 2</code></li>
<li><code>s</code>&nbsp;的部分 k 子序列为:
<ul>
<li><code>"<em><strong>ab</strong></em>bbdd"</code> -&gt; <code>"ab"</code>&nbsp;,美丽值为&nbsp;<code>f('a') + f('b') = 4</code></li>
<li><code>"<em><strong>a</strong></em>bbb<em><strong>d</strong></em>d"</code> -&gt; <code>"ad"</code>&nbsp;,美丽值为&nbsp;<code>f('a') + f('d') = 3</code></li>
<li><code>"a<em><strong>b</strong></em>bb<em><strong>d</strong></em>d"</code> -&gt; <code>"bd"</code>&nbsp;,美丽值为&nbsp;<code>f('b') + f('d') = 5</code></li>
</ul>
</li>
</ul>
<p>请你返回一个整数,表示所有 <strong>k 子序列&nbsp;</strong>里面 <strong>美丽值 </strong>&nbsp;<strong>最大值</strong>&nbsp;的子序列数目。由于答案可能很大,将结果对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;取余后返回。</p>
<p>一个字符串的子序列指的是从原字符串里面删除一些字符(也可能一个字符也不删除),不改变剩下字符顺序连接得到的新字符串。</p>
<p><strong>注意:</strong></p>
<ul>
<li><code>f(c)</code> 指的是字符&nbsp;<code>c</code>&nbsp;在字符串&nbsp;<code>s</code>&nbsp;的出现次数,不是在 k 子序列里的出现次数。</li>
<li>两个 k 子序列如果有任何一个字符在原字符串中的下标不同,则它们是两个不同的子序列。所以两个不同的 k 子序列可能产生相同的字符串。</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>s = "bcca", k = 2
<b>输出:</b>4
<b>解释:</b><span style="white-space: normal">s 中我们有 f('a') = 1 f('b') = 1 和 f('c') = 2 。</span>
s 的 k 子序列为:
<em><strong>bc</strong></em>ca ,美丽值为 f('b') + f('c') = 3
<em><strong>b</strong></em>c<em><strong>c</strong></em>a ,美丽值为 f('b') + f('c') = 3
<em><strong>b</strong></em>cc<em><strong>a</strong></em> ,美丽值为 f('b') + f('a') = 2
b<em><strong>c</strong></em>c<em><strong>a</strong></em><strong> </strong>,美丽值为 f('c') + f('a') = 3
bc<em><strong>ca</strong></em> ,美丽值为 f('c') + f('a') = 3
总共有 4 个 k 子序列美丽值为最大值 3 。
所以答案为 4 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>s = "abbcd", k = 4
<b>输出:</b>2
<b>解释:</b><span style="white-space: normal">s 中我们有 f('a') = 1 f('b') = 2&nbsp;f('c') = 1&nbsp;</span> f('d') = 1 。
s 的 k 子序列为:
<em><strong>ab</strong></em>b<em><strong>cd</strong></em> ,美丽值为 f('a') + f('b') + f('c') + f('d') = 5
<span style="white-space: normal;"><b><i>a</i></b></span>b<em><strong>bcd</strong></em> ,美丽值为 f('a') + f('b') + f('c') + f('d') = 5
总共有 2 个 k 子序列美丽值为最大值 5 。
所以答案为 2 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= s.length</code></li>
<li><code>s</code>&nbsp;只包含小写英文字母。</li>
</ul>

View File

@ -0,0 +1,31 @@
<p>给你两个正整数 <code>low</code><code>high</code></p>
<p>对于一个由 <code>2 * n</code> 位数字组成的整数 <code>x</code> ,如果其前 <code>n</code> 位数字之和与后 <code>n</code> 位数字之和相等,则认为这个数字是一个对称整数。</p>
<p>返回在 <code>[low, high]</code> 范围内的 <strong>对称整数的数目</strong></p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>low = 1, high = 100
<strong>输出:</strong>9
<strong>解释:</strong>在 1 到 100 范围内共有 9 个对称整数11、22、33、44、55、66、77、88 和 99 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>low = 1200, high = 1230
<strong>输出:</strong>4
<strong>解释:</strong>在 1200 到 1230 范围内共有 4 个对称整数1203、1212、1221 和 1230 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= low &lt;= high &lt;= 10<sup>4</sup></code></li>
</ul>

View File

@ -0,0 +1,57 @@
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,以及整数 <code>modulo</code> 和整数 <code>k</code></p>
<p>请你找出并统计数组中 <strong>趣味子数组</strong> 的数目。</p>
<p>如果 <strong>子数组</strong> <code>nums[l..r]</code> 满足下述条件,则称其为 <strong>趣味子数组</strong> </p>
<ul>
<li>在范围 <code>[l, r]</code> 内,设 <code>cnt</code> 为满足 <code>nums[i] % modulo == k</code> 的索引 <code>i</code> 的数量。并且 <code>cnt % modulo == k</code></li>
</ul>
<p>以整数形式表示并返回趣味子数组的数目。<em> </em></p>
<p><span><strong>注意:</strong>子数组是数组中的一个连续非空的元素序列。</span></p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [3,2,4], modulo = 2, k = 1
<strong>输出:</strong>3
<strong>解释:</strong>在这个示例中,趣味子数组分别是:
子数组 nums[0..0] ,也就是 [3] 。
- 在范围 [0, 0] 内,只存在 1 个下标 i = 0 满足 nums[i] % modulo == k 。
- 因此 cnt = 1 ,且 cnt % modulo == k 。
子数组 nums[0..1] ,也就是 [3,2] 。
- 在范围 [0, 1] 内,只存在 1 个下标 i = 0 满足 nums[i] % modulo == k 。
- 因此 cnt = 1 ,且 cnt % modulo == k 。
子数组 nums[0..2] ,也就是 [3,2,4] 。
- 在范围 [0, 2] 内,只存在 1 个下标 i = 0 满足 nums[i] % modulo == k 。
- 因此 cnt = 1 ,且 cnt % modulo == k 。
可以证明不存在其他趣味子数组。因此,答案为 3 。</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [3,1,9,6], modulo = 3, k = 0
<strong>输出:</strong>2
<strong>解释:</strong>在这个示例中,趣味子数组分别是:
子数组 nums[0..3] ,也就是 [3,1,9,6] 。
- 在范围 [0, 3] 内,只存在 3 个下标 i = 0, 2, 3 满足 nums[i] % modulo == k 。
- 因此 cnt = 3 ,且 cnt % modulo == k 。
子数组 nums[1..1] ,也就是 [1] 。
- 在范围 [1, 1] 内,不存在下标满足 nums[i] % modulo == k 。
- 因此 cnt = 0 ,且 cnt % modulo == k 。
可以证明不存在其他趣味子数组,因此答案为 2 。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5 </sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= modulo &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= k &lt; modulo</code></li>
</ul>

View File

@ -0,0 +1,54 @@
<p>现有一棵由 <code>n</code> 个节点组成的无向树,节点按从 <code>0</code><code>n - 1</code> 编号。给你一个整数 <code>n</code> 和一个长度为 <code>n - 1</code> 的二维整数数组 <code>edges</code> ,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> 表示树中存在一条位于节点 <code>u<sub>i</sub></code> 和节点 <code>v<sub>i</sub></code> 之间、权重为 <code>w<sub>i</sub></code> 的边。</p>
<p>另给你一个长度为 <code>m</code> 的二维整数数组 <code>queries</code> ,其中 <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 。对于每条查询,请你找出使从 <code>a<sub>i</sub></code><code>b<sub>i</sub></code> 路径上每条边的权重相等所需的 <strong>最小操作次数</strong> 。在一次操作中,你可以选择树上的任意一条边,并将其权重更改为任意值。</p>
<p><strong>注意:</strong></p>
<ul>
<li>查询之间 <strong>相互独立</strong> 的,这意味着每条新的查询时,树都会回到 <strong>初始状态</strong></li>
<li><code>a<sub>i</sub></code><code>b<sub>i</sub></code>的路径是一个由 <strong>不同</strong> 节点组成的序列,从节点 <code>a<sub>i</sub></code> 开始,到节点 <code>b<sub>i</sub></code> 结束,且序列中相邻的两个节点在树中共享一条边。</li>
</ul>
<p>返回一个长度为 <code>m</code> 的数组 <code>answer</code> ,其中 <code>answer[i]</code> 是第 <code>i</code> 条查询的答案。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png" style="width: 339px; height: 344px;" />
<pre>
<strong>输入:</strong>n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6],[2,6],[0,6]]
<strong>输出:</strong>[0,0,1,3]
<strong>解释:</strong>第 1 条查询,从节点 0 到节点 3 的路径中的所有边的权重都是 1 。因此,答案为 0 。
第 2 条查询,从节点 3 到节点 6 的路径中的所有边的权重都是 2 。因此,答案为 0 。
第 3 条查询,将边 [2,3] 的权重变更为 2 。在这次操作之后,从节点 2 到节点 6 的路径中的所有边的权重都是 2 。因此,答案为 1 。
第 4 条查询,将边 [0,1]、[1,2]、[2,3] 的权重变更为 2 。在这次操作之后,从节点 0 到节点 6 的路径中的所有边的权重都是 2 。因此,答案为 3 。
对于每条查询 queries[i] ,可以证明 answer[i] 是使从 a<sub>i</sub> 到 b<sub>i</sub> 的路径中的所有边的权重相等的最小操作次数。
</pre>
<p><strong class="example">示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png" style="width: 472px; height: 370px;" />
<pre>
<strong>输入:</strong>n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], queries = [[4,6],[0,4],[6,5],[7,4]]
<strong>输出:</strong>[1,2,2,3]
<strong>解释:</strong>第 1 条查询,将边 [1,3] 的权重变更为 6 。在这次操作之后,从节点 4 到节点 6 的路径中的所有边的权重都是 6 。因此,答案为 1 。
第 2 条查询,将边 [0,3]、[3,1] 的权重变更为 6 。在这次操作之后,从节点 0 到节点 4 的路径中的所有边的权重都是 6 。因此,答案为 2 。
第 3 条查询,将边 [1,3]、[5,2] 的权重变更为 6 。在这次操作之后,从节点 6 到节点 5 的路径中的所有边的权重都是 6 。因此,答案为 2 。
第 4 条查询,将边 [0,7]、[0,3]、[1,3] 的权重变更为 6 。在这次操作之后,从节点 7 到节点 4 的路径中的所有边的权重都是 6 。因此,答案为 3 。
对于每条查询 queries[i] ,可以证明 answer[i] 是使从 a<sub>i</sub> 到 b<sub>i</sub> 的路径中的所有边的权重相等的最小操作次数。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 3</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>1 &lt;= w<sub>i</sub> &lt;= 26</code></li>
<li>生成的输入满足 <code>edges</code> 表示一棵有效的树</li>
<li><code>1 &lt;= queries.length == m &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
</ul>

View File

@ -0,0 +1,41 @@
<p>You are given an integer array <code>nums</code> and two positive integers <code>m</code> and <code>k</code>.</p>
<p>Return <em>the <strong>maximum sum</strong> out of all <strong>almost unique</strong> subarrays of length </em><code>k</code><em> of</em> <code>nums</code>. If no such subarray exists, return <code>0</code>.</p>
<p>A subarray of <code>nums</code> is <strong>almost unique</strong> if it contains at least <code>m</code> distinct elements.</p>
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,6,7,3,1,7], m = 3, k = 4
<strong>Output:</strong> 18
<strong>Explanation:</strong> There are 3 almost unique subarrays of size <code>k = 4</code>. These subarrays are [2, 6, 7, 3], [6, 7, 3, 1], and [7, 3, 1, 7]. Among these subarrays, the one with the maximum sum is [2, 6, 7, 3] which has a sum of 18.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,9,9,2,4,5,4], m = 1, k = 3
<strong>Output:</strong> 23
<strong>Explanation:</strong> There are 5 almost unique subarrays of size k. These subarrays are [5, 9, 9], [9, 9, 2], [9, 2, 4], [2, 4, 5], and [4, 5, 4]. Among these subarrays, the one with the maximum sum is [5, 9, 9] which has a sum of 23.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,1,2,1,2,1], m = 3, k = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no subarrays of size <code>k = 3</code> that contain at least <code>m = 3</code> distinct elements in the given array [1,2,1,2,1,2,1]. Therefore, no almost unique subarrays exist, and the maximum sum is 0.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= m &lt;= k &lt;= nums.length</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@ -0,0 +1,36 @@
<p>You are given two strings <code>s1</code> and <code>s2</code>, both of length <code>4</code>, consisting of <strong>lowercase</strong> English letters.</p>
<p>You can apply the following operation on any of the two strings <strong>any</strong> number of times:</p>
<ul>
<li>Choose any two indices <code>i</code> and <code>j</code> such that <code>j - i = 2</code>, then <strong>swap</strong> the two characters at those indices in the string.</li>
</ul>
<p>Return <code>true</code><em> if you can make the strings </em><code>s1</code><em> and </em><code>s2</code><em> equal, and </em><code>false</code><em> otherwise</em>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;abcd&quot;, s2 = &quot;cdab&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> We can do the following operations on s1:
- Choose the indices i = 0, j = 2. The resulting string is s1 = &quot;cbad&quot;.
- Choose the indices i = 1, j = 3. The resulting string is s1 = &quot;cdab&quot; = s2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;abcd&quot;, s2 = &quot;dacb&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> It is not possible to make the two strings equal.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>s1.length == s2.length == 4</code></li>
<li><code>s1</code> and <code>s2</code> consist only of lowercase English letters.</li>
</ul>

View File

@ -0,0 +1,38 @@
<p>You are given two strings <code>s1</code> and <code>s2</code>, both of length <code>n</code>, consisting of <strong>lowercase</strong> English letters.</p>
<p>You can apply the following operation on <strong>any</strong> of the two strings <strong>any</strong> number of times:</p>
<ul>
<li>Choose any two indices <code>i</code> and <code>j</code> such that <code>i &lt; j</code> and the difference <code>j - i</code> is <strong>even</strong>, then <strong>swap</strong> the two characters at those indices in the string.</li>
</ul>
<p>Return <code>true</code><em> if you can make the strings </em><code>s1</code><em> and </em><code>s2</code><em> equal, and&nbsp;</em><code>false</code><em> otherwise</em>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;abcdba&quot;, s2 = &quot;cabdab&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> We can apply the following operations on s1:
- Choose the indices i = 0, j = 2. The resulting string is s1 = &quot;cbadba&quot;.
- Choose the indices i = 2, j = 4. The resulting string is s1 = &quot;cbbdaa&quot;.
- Choose the indices i = 1, j = 5. The resulting string is s1 = &quot;cabdab&quot; = s2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;abe&quot;, s2 = &quot;bea&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> It is not possible to make the two strings equal.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == s1.length == s2.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>s1</code> and <code>s2</code> consist only of lowercase English letters.</li>
</ul>

View File

@ -0,0 +1,51 @@
<p>Given a function <code>fn</code>, an array of&nbsp;arguments&nbsp;<code>args</code>, and a timeout&nbsp;<code>t</code>&nbsp;in milliseconds, return a cancel function <code>cancelFn</code>.</p>
<p>After a delay of&nbsp;<code>t</code>,&nbsp;<code>fn</code>&nbsp;should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was invoked before the delay of <code>t</code> milliseconds elapses, specifically at <code>cancelT</code>&nbsp;ms.&nbsp;In that case,&nbsp;<code>fn</code> should never be called.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> fn = (x) =&gt; x * 5, args = [2], t = 20, cancelT = 50
<strong>Output:</strong> [{&quot;time&quot;: 20, &quot;returned&quot;: 10}]
<strong>Explanation:</strong>
const cancel = cancellable((x) =&gt; x * 5, [2], 20); // fn(2) called at t=20ms
setTimeout(cancel, 50);
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> fn = (x) =&gt; x**2, args = [2], t = 100, cancelT = 50
<strong>Output:</strong> []
<strong>Explanation:</strong>
const cancel = cancellable((x) =&gt; x**2, [2], 100); // fn(2) not called
setTimeout(cancel, 50);
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> fn = (x1, x2) =&gt; x1 * x2, args = [2,4], t = 30, cancelT = 100
<strong>Output:</strong> [{&quot;time&quot;: 30, &quot;returned&quot;: 8}]
<strong>Explanation:</strong>
const cancel = cancellable((x1, x2) =&gt; x1 * x2, [2,4], 30); // fn(2,4) called at t=30ms
setTimeout(cancel, 100);
The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>fn is a function</code></li>
<li><code>args is a valid JSON array</code></li>
<li><code>1 &lt;= args.length &lt;= 10</code></li>
<li><code><font face="monospace">20 &lt;= t &lt;= 1000</font></code></li>
<li><code><font face="monospace">10 &lt;= cancelT &lt;= 1000</font></code></li>
</ul>

View File

@ -0,0 +1,43 @@
<p>You are given a <strong>0-indexed</strong> string <code>num</code> representing a non-negative integer.</p>
<p>In one operation, you can pick any digit of <code>num</code> and delete it. Note that if you delete all the digits of <code>num</code>, <code>num</code> becomes <code>0</code>.</p>
<p>Return <em>the <strong>minimum number of operations</strong> required to make</em> <code>num</code> <i>special</i>.</p>
<p>An integer <code>x</code> is considered <strong>special</strong> if it is divisible by <code>25</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = &quot;2245047&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> Delete digits num[5] and num[6]. The resulting number is &quot;22450&quot; which is special since it is divisible by 25.
It can be shown that 2 is the minimum number of operations required to get a special number.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = &quot;2908305&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> Delete digits num[3], num[4], and num[6]. The resulting number is &quot;2900&quot; which is special since it is divisible by 25.
It can be shown that 3 is the minimum number of operations required to get a special number.</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = &quot;10&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> Delete digit num[0]. The resulting number is &quot;0&quot; which is special since it is divisible by 25.
It can be shown that 1 is the minimum number of operations required to get a special number.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num.length &lt;= 100</code></li>
<li><code>num</code> only consists of digits <code>&#39;0&#39;</code> through <code>&#39;9&#39;</code>.</li>
<li><code>num</code> does not contain any leading zeros.</li>
</ul>

View File

@ -0,0 +1,70 @@
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p>
<p>A <strong>k-subsequence</strong> is a <strong>subsequence</strong> of <code>s</code>, having length <code>k</code>, and all its characters are <strong>unique</strong>, <strong>i.e</strong>., every character occurs once.</p>
<p>Let <code>f(c)</code> denote the number of times the character <code>c</code> occurs in <code>s</code>.</p>
<p>The <strong>beauty</strong> of a <strong>k-subsequence</strong> is the <strong>sum</strong> of <code>f(c)</code> for every character <code>c</code> in the k-subsequence.</p>
<p>For example, consider <code>s = &quot;abbbdd&quot;</code> and <code>k = 2</code>:</p>
<ul>
<li><code>f(&#39;a&#39;) = 1</code>, <code>f(&#39;b&#39;) = 3</code>, <code>f(&#39;d&#39;) = 2</code></li>
<li>Some k-subsequences of <code>s</code> are:
<ul>
<li><code>&quot;<u><strong>ab</strong></u>bbdd&quot;</code> -&gt; <code>&quot;ab&quot;</code> having a beauty of <code>f(&#39;a&#39;) + f(&#39;b&#39;) = 4</code></li>
<li><code>&quot;<u><strong>a</strong></u>bbb<strong><u>d</u></strong>d&quot;</code> -&gt; <code>&quot;ad&quot;</code> having a beauty of <code>f(&#39;a&#39;) + f(&#39;d&#39;) = 3</code></li>
<li><code>&quot;a<strong><u>b</u></strong>bb<u><strong>d</strong></u>d&quot;</code> -&gt; <code>&quot;bd&quot;</code> having a beauty of <code>f(&#39;b&#39;) + f(&#39;d&#39;) = 5</code></li>
</ul>
</li>
</ul>
<p>Return <em>an integer denoting the number of k-subsequences </em><em>whose <strong>beauty</strong> is the <strong>maximum</strong> among all <strong>k-subsequences</strong></em>. Since the answer may be too large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
<p>A subsequence of a string is a new string formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.</p>
<p><strong>Notes</strong></p>
<ul>
<li><code>f(c)</code> is the number of times a character <code>c</code> occurs in <code>s</code>, not a k-subsequence.</li>
<li>Two k-subsequences are considered different if one is formed by an index that is not present in the other. So, two k-subsequences may form the same string.</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;bcca&quot;, k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> <span style="white-space: normal">From s we have f(&#39;a&#39;) = 1, f(&#39;b&#39;) = 1, and f(&#39;c&#39;) = 2.</span>
The k-subsequences of s are:
<strong><u>bc</u></strong>ca having a beauty of f(&#39;b&#39;) + f(&#39;c&#39;) = 3
<strong><u>b</u></strong>c<u><strong>c</strong></u>a having a beauty of f(&#39;b&#39;) + f(&#39;c&#39;) = 3
<strong><u>b</u></strong>cc<strong><u>a</u></strong> having a beauty of f(&#39;b&#39;) + f(&#39;a&#39;) = 2
b<strong><u>c</u></strong>c<u><strong>a</strong></u><strong> </strong>having a beauty of f(&#39;c&#39;) + f(&#39;a&#39;) = 3
bc<strong><u>ca</u></strong> having a beauty of f(&#39;c&#39;) + f(&#39;a&#39;) = 3
There are 4 k-subsequences that have the maximum beauty, 3.
Hence, the answer is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;abbcd&quot;, k = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong> From s we have f(&#39;a&#39;) = 1, f(&#39;b&#39;) = 2, f(&#39;c&#39;) = 1, and f(&#39;d&#39;) = 1.
The k-subsequences of s are:
<u><strong>ab</strong></u>b<strong><u>cd</u></strong> having a beauty of f(&#39;a&#39;) + f(&#39;b&#39;) + f(&#39;c&#39;) + f(&#39;d&#39;) = 5
<u style="white-space: normal;"><strong>a</strong></u>b<u><strong>bcd</strong></u> having a beauty of f(&#39;a&#39;) + f(&#39;b&#39;) + f(&#39;c&#39;) + f(&#39;d&#39;) = 5
There are 2 k-subsequences that have the maximum beauty, 5.
Hence, the answer is 2.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= s.length</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>

View File

@ -0,0 +1,29 @@
<p>You are given two positive integers <code>low</code> and <code>high</code>.</p>
<p>An integer <code>x</code> consisting of <code>2 * n</code> digits is <strong>symmetric</strong> if the sum of the first <code>n</code> digits of <code>x</code> is equal to the sum of the last <code>n</code> digits of <code>x</code>. Numbers with an odd number of digits are never symmetric.</p>
<p>Return <em>the <strong>number of symmetric</strong> integers in the range</em> <code>[low, high]</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 1, high = 100
<strong>Output:</strong> 9
<strong>Explanation:</strong> There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 1200, high = 1230
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= low &lt;= high &lt;= 10<sup>4</sup></code></li>
</ul>

View File

@ -0,0 +1,55 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, an integer <code>modulo</code>, and an integer <code>k</code>.</p>
<p>Your task is to find the count of subarrays that are <strong>interesting</strong>.</p>
<p>A <strong>subarray</strong> <code>nums[l..r]</code> is <strong>interesting</strong> if the following condition holds:</p>
<ul>
<li>Let <code>cnt</code> be the number of indices <code>i</code> in the range <code>[l, r]</code> such that <code>nums[i] % modulo == k</code>. Then, <code>cnt % modulo == k</code>.</li>
</ul>
<p>Return <em>an integer denoting the count of interesting subarrays. </em></p>
<p><span><strong>Note:</strong> A subarray is <em>a contiguous non-empty sequence of elements within an array</em>.</span></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,4], modulo = 2, k = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> In this example the interesting subarrays are:
The subarray nums[0..0] which is [3].
- There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k.
- Hence, cnt = 1 and cnt % modulo == k.
The subarray nums[0..1] which is [3,2].
- There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.
- Hence, cnt = 1 and cnt % modulo == k.
The subarray nums[0..2] which is [3,2,4].
- There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k.
- Hence, cnt = 1 and cnt % modulo == k.
It can be shown that there are no other interesting subarrays. So, the answer is 3.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,9,6], modulo = 3, k = 0
<strong>Output:</strong> 2
<strong>Explanation: </strong>In this example the interesting subarrays are:
The subarray nums[0..3] which is [3,1,9,6].
- There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k.
- Hence, cnt = 3 and cnt % modulo == k.
The subarray nums[1..1] which is [1].
- There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k.
- Hence, cnt = 0 and cnt % modulo == k.
It can be shown that there are no other interesting subarrays. So, the answer is 2.</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5 </sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= modulo &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= k &lt; modulo</code></li>
</ul>

View File

@ -0,0 +1,52 @@
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given the integer <code>n</code> and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code> in the tree.</p>
<p>You are also given a 2D integer array <code>queries</code> of length <code>m</code>, where <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>. For each query, find the <strong>minimum number of operations</strong> required to make the weight of every edge on the path from <code>a<sub>i</sub></code> to <code>b<sub>i</sub></code> equal. In one operation, you can choose any edge of the tree and change its weight to any value.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>Queries are <strong>independent</strong> of each other, meaning that the tree returns to its <strong>initial state</strong> on each new query.</li>
<li>The path from <code>a<sub>i</sub></code> to <code>b<sub>i</sub></code> is a sequence of <strong>distinct</strong> nodes starting with node <code>a<sub>i</sub></code> and ending with node <code>b<sub>i</sub></code> such that every two adjacent nodes in the sequence share an edge in the tree.</li>
</ul>
<p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where</em> <code>answer[i]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png" style="width: 339px; height: 344px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6],[2,6],[0,6]]
<strong>Output:</strong> [0,0,1,3]
<strong>Explanation:</strong> In the first query, all the edges in the path from 0 to 3 have a weight of 1. Hence, the answer is 0.
In the second query, all the edges in the path from 3 to 6 have a weight of 2. Hence, the answer is 0.
In the third query, we change the weight of edge [2,3] to 2. After this operation, all the edges in the path from 2 to 6 have a weight of 2. Hence, the answer is 1.
In the fourth query, we change the weights of edges [0,1], [1,2] and [2,3] to 2. After these operations, all the edges in the path from 0 to 6 have a weight of 2. Hence, the answer is 3.
For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from a<sub>i</sub> to b<sub>i</sub>.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png" style="width: 472px; height: 370px;" />
<pre>
<strong>Input:</strong> n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], queries = [[4,6],[0,4],[6,5],[7,4]]
<strong>Output:</strong> [1,2,2,3]
<strong>Explanation:</strong> In the first query, we change the weight of edge [1,3] to 6. After this operation, all the edges in the path from 4 to 6 have a weight of 6. Hence, the answer is 1.
In the second query, we change the weight of edges [0,3] and [3,1] to 6. After these operations, all the edges in the path from 0 to 4 have a weight of 6. Hence, the answer is 2.
In the third query, we change the weight of edges [1,3] and [5,2] to 6. After these operations, all the edges in the path from 6 to 5 have a weight of 6. Hence, the answer is 2.
In the fourth query, we change the weights of edges [0,7], [0,3] and [1,3] to 6. After these operations, all the edges in the path from 7 to 4 have a weight of 6. Hence, the answer is 3.
For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from a<sub>i</sub> to b<sub>i</sub>.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 3</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>1 &lt;= w<sub>i</sub> &lt;= 26</code></li>
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
<li><code>1 &lt;= queries.length == m &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
</ul>

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,36 @@
<p>You are given two strings <code>s1</code> and <code>s2</code>, both of length <code>4</code>, consisting of <strong>lowercase</strong> English letters.</p>
<p>You can apply the following operation on any of the two strings <strong>any</strong> number of times:</p>
<ul>
<li>Choose any two indices <code>i</code> and <code>j</code> such that <code>j - i = 2</code>, then <strong>swap</strong> the two characters at those indices in the string.</li>
</ul>
<p>Return <code>true</code><em> if you can make the strings </em><code>s1</code><em> and </em><code>s2</code><em> equal, and </em><code>false</code><em> otherwise</em>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;abcd&quot;, s2 = &quot;cdab&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> We can do the following operations on s1:
- Choose the indices i = 0, j = 2. The resulting string is s1 = &quot;cbad&quot;.
- Choose the indices i = 1, j = 3. The resulting string is s1 = &quot;cdab&quot; = s2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;abcd&quot;, s2 = &quot;dacb&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> It is not possible to make the two strings equal.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>s1.length == s2.length == 4</code></li>
<li><code>s1</code> and <code>s2</code> consist only of lowercase English letters.</li>
</ul>

View File

@ -0,0 +1,38 @@
<p>You are given two strings <code>s1</code> and <code>s2</code>, both of length <code>n</code>, consisting of <strong>lowercase</strong> English letters.</p>
<p>You can apply the following operation on <strong>any</strong> of the two strings <strong>any</strong> number of times:</p>
<ul>
<li>Choose any two indices <code>i</code> and <code>j</code> such that <code>i &lt; j</code> and the difference <code>j - i</code> is <strong>even</strong>, then <strong>swap</strong> the two characters at those indices in the string.</li>
</ul>
<p>Return <code>true</code><em> if you can make the strings </em><code>s1</code><em> and </em><code>s2</code><em> equal, and&nbsp;</em><code>false</code><em> otherwise</em>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;abcdba&quot;, s2 = &quot;cabdab&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> We can apply the following operations on s1:
- Choose the indices i = 0, j = 2. The resulting string is s1 = &quot;cbadba&quot;.
- Choose the indices i = 2, j = 4. The resulting string is s1 = &quot;cbbdaa&quot;.
- Choose the indices i = 1, j = 5. The resulting string is s1 = &quot;cabdab&quot; = s2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s1 = &quot;abe&quot;, s2 = &quot;bea&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> It is not possible to make the two strings equal.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == s1.length == s2.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>s1</code> and <code>s2</code> consist only of lowercase English letters.</li>
</ul>

View File

@ -0,0 +1,70 @@
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p>
<p>A <strong>k-subsequence</strong> is a <strong>subsequence</strong> of <code>s</code>, having length <code>k</code>, and all its characters are <strong>unique</strong>, <strong>i.e</strong>., every character occurs once.</p>
<p>Let <code>f(c)</code> denote the number of times the character <code>c</code> occurs in <code>s</code>.</p>
<p>The <strong>beauty</strong> of a <strong>k-subsequence</strong> is the <strong>sum</strong> of <code>f(c)</code> for every character <code>c</code> in the k-subsequence.</p>
<p>For example, consider <code>s = &quot;abbbdd&quot;</code> and <code>k = 2</code>:</p>
<ul>
<li><code>f(&#39;a&#39;) = 1</code>, <code>f(&#39;b&#39;) = 3</code>, <code>f(&#39;d&#39;) = 2</code></li>
<li>Some k-subsequences of <code>s</code> are:
<ul>
<li><code>&quot;<u><strong>ab</strong></u>bbdd&quot;</code> -&gt; <code>&quot;ab&quot;</code> having a beauty of <code>f(&#39;a&#39;) + f(&#39;b&#39;) = 4</code></li>
<li><code>&quot;<u><strong>a</strong></u>bbb<strong><u>d</u></strong>d&quot;</code> -&gt; <code>&quot;ad&quot;</code> having a beauty of <code>f(&#39;a&#39;) + f(&#39;d&#39;) = 3</code></li>
<li><code>&quot;a<strong><u>b</u></strong>bb<u><strong>d</strong></u>d&quot;</code> -&gt; <code>&quot;bd&quot;</code> having a beauty of <code>f(&#39;b&#39;) + f(&#39;d&#39;) = 5</code></li>
</ul>
</li>
</ul>
<p>Return <em>an integer denoting the number of k-subsequences </em><em>whose <strong>beauty</strong> is the <strong>maximum</strong> among all <strong>k-subsequences</strong></em>. Since the answer may be too large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>
<p>A subsequence of a string is a new string formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.</p>
<p><strong>Notes</strong></p>
<ul>
<li><code>f(c)</code> is the number of times a character <code>c</code> occurs in <code>s</code>, not a k-subsequence.</li>
<li>Two k-subsequences are considered different if one is formed by an index that is not present in the other. So, two k-subsequences may form the same string.</li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;bcca&quot;, k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> <span style="white-space: normal">From s we have f(&#39;a&#39;) = 1, f(&#39;b&#39;) = 1, and f(&#39;c&#39;) = 2.</span>
The k-subsequences of s are:
<strong><u>bc</u></strong>ca having a beauty of f(&#39;b&#39;) + f(&#39;c&#39;) = 3
<strong><u>b</u></strong>c<u><strong>c</strong></u>a having a beauty of f(&#39;b&#39;) + f(&#39;c&#39;) = 3
<strong><u>b</u></strong>cc<strong><u>a</u></strong> having a beauty of f(&#39;b&#39;) + f(&#39;a&#39;) = 2
b<strong><u>c</u></strong>c<u><strong>a</strong></u><strong> </strong>having a beauty of f(&#39;c&#39;) + f(&#39;a&#39;) = 3
bc<strong><u>ca</u></strong> having a beauty of f(&#39;c&#39;) + f(&#39;a&#39;) = 3
There are 4 k-subsequences that have the maximum beauty, 3.
Hence, the answer is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;abbcd&quot;, k = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong> From s we have f(&#39;a&#39;) = 1, f(&#39;b&#39;) = 2, f(&#39;c&#39;) = 1, and f(&#39;d&#39;) = 1.
The k-subsequences of s are:
<u><strong>ab</strong></u>b<strong><u>cd</u></strong> having a beauty of f(&#39;a&#39;) + f(&#39;b&#39;) + f(&#39;c&#39;) + f(&#39;d&#39;) = 5
<u style="white-space: normal;"><strong>a</strong></u>b<u><strong>bcd</strong></u> having a beauty of f(&#39;a&#39;) + f(&#39;b&#39;) + f(&#39;c&#39;) + f(&#39;d&#39;) = 5
There are 2 k-subsequences that have the maximum beauty, 5.
Hence, the answer is 2.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= s.length</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>

View File

@ -0,0 +1,55 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, an integer <code>modulo</code>, and an integer <code>k</code>.</p>
<p>Your task is to find the count of subarrays that are <strong>interesting</strong>.</p>
<p>A <strong>subarray</strong> <code>nums[l..r]</code> is <strong>interesting</strong> if the following condition holds:</p>
<ul>
<li>Let <code>cnt</code> be the number of indices <code>i</code> in the range <code>[l, r]</code> such that <code>nums[i] % modulo == k</code>. Then, <code>cnt % modulo == k</code>.</li>
</ul>
<p>Return <em>an integer denoting the count of interesting subarrays. </em></p>
<p><span><strong>Note:</strong> A subarray is <em>a contiguous non-empty sequence of elements within an array</em>.</span></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,4], modulo = 2, k = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> In this example the interesting subarrays are:
The subarray nums[0..0] which is [3].
- There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k.
- Hence, cnt = 1 and cnt % modulo == k.
The subarray nums[0..1] which is [3,2].
- There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.
- Hence, cnt = 1 and cnt % modulo == k.
The subarray nums[0..2] which is [3,2,4].
- There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k.
- Hence, cnt = 1 and cnt % modulo == k.
It can be shown that there are no other interesting subarrays. So, the answer is 3.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,9,6], modulo = 3, k = 0
<strong>Output:</strong> 2
<strong>Explanation: </strong>In this example the interesting subarrays are:
The subarray nums[0..3] which is [3,1,9,6].
- There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k.
- Hence, cnt = 3 and cnt % modulo == k.
The subarray nums[1..1] which is [1].
- There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k.
- Hence, cnt = 0 and cnt % modulo == k.
It can be shown that there are no other interesting subarrays. So, the answer is 2.</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5 </sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= modulo &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= k &lt; modulo</code></li>
</ul>

View File

@ -0,0 +1,29 @@
<p>You are given two positive integers <code>low</code> and <code>high</code>.</p>
<p>An integer <code>x</code> consisting of <code>2 * n</code> digits is <strong>symmetric</strong> if the sum of the first <code>n</code> digits of <code>x</code> is equal to the sum of the last <code>n</code> digits of <code>x</code>. Numbers with an odd number of digits are never symmetric.</p>
<p>Return <em>the <strong>number of symmetric</strong> integers in the range</em> <code>[low, high]</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 1, high = 100
<strong>Output:</strong> 9
<strong>Explanation:</strong> There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 1200, high = 1230
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= low &lt;= high &lt;= 10<sup>4</sup></code></li>
</ul>

View File

@ -0,0 +1,41 @@
<p>You are given an integer array <code>nums</code> and two positive integers <code>m</code> and <code>k</code>.</p>
<p>Return <em>the <strong>maximum sum</strong> out of all <strong>almost unique</strong> subarrays of length </em><code>k</code><em> of</em> <code>nums</code>. If no such subarray exists, return <code>0</code>.</p>
<p>A subarray of <code>nums</code> is <strong>almost unique</strong> if it contains at least <code>m</code> distinct elements.</p>
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,6,7,3,1,7], m = 3, k = 4
<strong>Output:</strong> 18
<strong>Explanation:</strong> There are 3 almost unique subarrays of size <code>k = 4</code>. These subarrays are [2, 6, 7, 3], [6, 7, 3, 1], and [7, 3, 1, 7]. Among these subarrays, the one with the maximum sum is [2, 6, 7, 3] which has a sum of 18.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,9,9,2,4,5,4], m = 1, k = 3
<strong>Output:</strong> 23
<strong>Explanation:</strong> There are 5 almost unique subarrays of size k. These subarrays are [5, 9, 9], [9, 9, 2], [9, 2, 4], [2, 4, 5], and [4, 5, 4]. Among these subarrays, the one with the maximum sum is [5, 9, 9] which has a sum of 23.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,1,2,1,2,1], m = 3, k = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no subarrays of size <code>k = 3</code> that contain at least <code>m = 3</code> distinct elements in the given array [1,2,1,2,1,2,1]. Therefore, no almost unique subarrays exist, and the maximum sum is 0.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= m &lt;= k &lt;= nums.length</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@ -0,0 +1,52 @@
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given the integer <code>n</code> and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code> in the tree.</p>
<p>You are also given a 2D integer array <code>queries</code> of length <code>m</code>, where <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>. For each query, find the <strong>minimum number of operations</strong> required to make the weight of every edge on the path from <code>a<sub>i</sub></code> to <code>b<sub>i</sub></code> equal. In one operation, you can choose any edge of the tree and change its weight to any value.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>Queries are <strong>independent</strong> of each other, meaning that the tree returns to its <strong>initial state</strong> on each new query.</li>
<li>The path from <code>a<sub>i</sub></code> to <code>b<sub>i</sub></code> is a sequence of <strong>distinct</strong> nodes starting with node <code>a<sub>i</sub></code> and ending with node <code>b<sub>i</sub></code> such that every two adjacent nodes in the sequence share an edge in the tree.</li>
</ul>
<p>Return <em>an array </em><code>answer</code><em> of length </em><code>m</code><em> where</em> <code>answer[i]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query.</em></p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-6-1.png" style="width: 339px; height: 344px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6],[2,6],[0,6]]
<strong>Output:</strong> [0,0,1,3]
<strong>Explanation:</strong> In the first query, all the edges in the path from 0 to 3 have a weight of 1. Hence, the answer is 0.
In the second query, all the edges in the path from 3 to 6 have a weight of 2. Hence, the answer is 0.
In the third query, we change the weight of edge [2,3] to 2. After this operation, all the edges in the path from 2 to 6 have a weight of 2. Hence, the answer is 1.
In the fourth query, we change the weights of edges [0,1], [1,2] and [2,3] to 2. After these operations, all the edges in the path from 0 to 6 have a weight of 2. Hence, the answer is 3.
For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from a<sub>i</sub> to b<sub>i</sub>.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/11/graph-9-1.png" style="width: 472px; height: 370px;" />
<pre>
<strong>Input:</strong> n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]], queries = [[4,6],[0,4],[6,5],[7,4]]
<strong>Output:</strong> [1,2,2,3]
<strong>Explanation:</strong> In the first query, we change the weight of edge [1,3] to 6. After this operation, all the edges in the path from 4 to 6 have a weight of 6. Hence, the answer is 1.
In the second query, we change the weight of edges [0,3] and [3,1] to 6. After these operations, all the edges in the path from 0 to 4 have a weight of 6. Hence, the answer is 2.
In the third query, we change the weight of edges [1,3] and [5,2] to 6. After these operations, all the edges in the path from 6 to 5 have a weight of 6. Hence, the answer is 2.
In the fourth query, we change the weights of edges [0,7], [0,3] and [1,3] to 6. After these operations, all the edges in the path from 7 to 4 have a weight of 6. Hence, the answer is 3.
For each queries[i], it can be shown that answer[i] is the minimum number of operations needed to equalize all the edge weights in the path from a<sub>i</sub> to b<sub>i</sub>.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 3</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>1 &lt;= w<sub>i</sub> &lt;= 26</code></li>
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
<li><code>1 &lt;= queries.length == m &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
</ul>

View File

@ -0,0 +1,43 @@
<p>You are given a <strong>0-indexed</strong> string <code>num</code> representing a non-negative integer.</p>
<p>In one operation, you can pick any digit of <code>num</code> and delete it. Note that if you delete all the digits of <code>num</code>, <code>num</code> becomes <code>0</code>.</p>
<p>Return <em>the <strong>minimum number of operations</strong> required to make</em> <code>num</code> <i>special</i>.</p>
<p>An integer <code>x</code> is considered <strong>special</strong> if it is divisible by <code>25</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = &quot;2245047&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> Delete digits num[5] and num[6]. The resulting number is &quot;22450&quot; which is special since it is divisible by 25.
It can be shown that 2 is the minimum number of operations required to get a special number.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = &quot;2908305&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> Delete digits num[3], num[4], and num[6]. The resulting number is &quot;2900&quot; which is special since it is divisible by 25.
It can be shown that 3 is the minimum number of operations required to get a special number.</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = &quot;10&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> Delete digit num[0]. The resulting number is &quot;0&quot; which is special since it is divisible by 25.
It can be shown that 1 is the minimum number of operations required to get a special number.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num.length &lt;= 100</code></li>
<li><code>num</code> only consists of digits <code>&#39;0&#39;</code> through <code>&#39;9&#39;</code>.</li>
<li><code>num</code> does not contain any leading zeros.</li>
</ul>