mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
195 lines
21 KiB
JSON
195 lines
21 KiB
JSON
{
|
|
"data": {
|
|
"question": {
|
|
"questionId": "3057",
|
|
"questionFrontendId": "2842",
|
|
"boundTopicId": null,
|
|
"title": "Count K-Subsequences of a String With Maximum Beauty",
|
|
"titleSlug": "count-k-subsequences-of-a-string-with-maximum-beauty",
|
|
"content": "<p>You are given a string <code>s</code> and an integer <code>k</code>.</p>\n\n<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>\n\n<p>Let <code>f(c)</code> denote the number of times the character <code>c</code> occurs in <code>s</code>.</p>\n\n<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>\n\n<p>For example, consider <code>s = "abbbdd"</code> and <code>k = 2</code>:</p>\n\n<ul>\n\t<li><code>f('a') = 1</code>, <code>f('b') = 3</code>, <code>f('d') = 2</code></li>\n\t<li>Some k-subsequences of <code>s</code> are:\n\t<ul>\n\t\t<li><code>"<u><strong>ab</strong></u>bbdd"</code> -> <code>"ab"</code> having a beauty of <code>f('a') + f('b') = 4</code></li>\n\t\t<li><code>"<u><strong>a</strong></u>bbb<strong><u>d</u></strong>d"</code> -> <code>"ad"</code> having a beauty of <code>f('a') + f('d') = 3</code></li>\n\t\t<li><code>"a<strong><u>b</u></strong>bb<u><strong>d</strong></u>d"</code> -> <code>"bd"</code> having a beauty of <code>f('b') + f('d') = 5</code></li>\n\t</ul>\n\t</li>\n</ul>\n\n<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>\n\n<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>\n\n<p><strong>Notes</strong></p>\n\n<ul>\n\t<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>\n\t<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>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = "bcca", k = 2\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> <span style=\"white-space: normal\">From s we have f('a') = 1, f('b') = 1, and f('c') = 2.</span>\nThe k-subsequences of s are: \n<strong><u>bc</u></strong>ca having a beauty of f('b') + f('c') = 3 \n<strong><u>b</u></strong>c<u><strong>c</strong></u>a having a beauty of f('b') + f('c') = 3 \n<strong><u>b</u></strong>cc<strong><u>a</u></strong> having a beauty of f('b') + f('a') = 2 \nb<strong><u>c</u></strong>c<u><strong>a</strong></u><strong> </strong>having a beauty of f('c') + f('a') = 3\nbc<strong><u>ca</u></strong> having a beauty of f('c') + f('a') = 3 \nThere are 4 k-subsequences that have the maximum beauty, 3. \nHence, the answer is 4. \n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = "abbcd", k = 4\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> From s we have f('a') = 1, f('b') = 2, f('c') = 1, and f('d') = 1. \nThe k-subsequences of s are: \n<u><strong>ab</strong></u>b<strong><u>cd</u></strong> having a beauty of f('a') + f('b') + f('c') + f('d') = 5\n<u style=\"white-space: normal;\"><strong>a</strong></u>b<u><strong>bcd</strong></u> having a beauty of f('a') + f('b') + f('c') + f('d') = 5 \nThere are 2 k-subsequences that have the maximum beauty, 5. \nHence, the answer is 2. \n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= s.length <= 2 * 10<sup>5</sup></code></li>\n\t<li><code>1 <= k <= s.length</code></li>\n\t<li><code>s</code> consists only of lowercase English letters.</li>\n</ul>\n",
|
|
"translatedTitle": null,
|
|
"translatedContent": null,
|
|
"isPaidOnly": false,
|
|
"difficulty": "Hard",
|
|
"likes": 290,
|
|
"dislikes": 22,
|
|
"isLiked": null,
|
|
"similarQuestions": "[{\"title\": \"Distinct Subsequences II\", \"titleSlug\": \"distinct-subsequences-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]",
|
|
"exampleTestcases": "\"bcca\"\n2\n\"abbcd\"\n4",
|
|
"categoryTitle": "Algorithms",
|
|
"contributors": [],
|
|
"topicTags": [
|
|
{
|
|
"name": "Hash Table",
|
|
"slug": "hash-table",
|
|
"translatedName": null,
|
|
"__typename": "TopicTagNode"
|
|
},
|
|
{
|
|
"name": "Math",
|
|
"slug": "math",
|
|
"translatedName": null,
|
|
"__typename": "TopicTagNode"
|
|
},
|
|
{
|
|
"name": "String",
|
|
"slug": "string",
|
|
"translatedName": null,
|
|
"__typename": "TopicTagNode"
|
|
},
|
|
{
|
|
"name": "Greedy",
|
|
"slug": "greedy",
|
|
"translatedName": null,
|
|
"__typename": "TopicTagNode"
|
|
},
|
|
{
|
|
"name": "Combinatorics",
|
|
"slug": "combinatorics",
|
|
"translatedName": null,
|
|
"__typename": "TopicTagNode"
|
|
}
|
|
],
|
|
"companyTagStats": null,
|
|
"codeSnippets": [
|
|
{
|
|
"lang": "C++",
|
|
"langSlug": "cpp",
|
|
"code": "class Solution {\npublic:\n int countKSubsequencesWithMaxBeauty(string s, int k) {\n \n }\n};",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Java",
|
|
"langSlug": "java",
|
|
"code": "class Solution {\n public int countKSubsequencesWithMaxBeauty(String s, int k) {\n \n }\n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Python",
|
|
"langSlug": "python",
|
|
"code": "class Solution(object):\n def countKSubsequencesWithMaxBeauty(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n ",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Python3",
|
|
"langSlug": "python3",
|
|
"code": "class Solution:\n def countKSubsequencesWithMaxBeauty(self, s: str, k: int) -> int:\n ",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "C",
|
|
"langSlug": "c",
|
|
"code": "int countKSubsequencesWithMaxBeauty(char* s, int k){\n\n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "C#",
|
|
"langSlug": "csharp",
|
|
"code": "public class Solution {\n public int CountKSubsequencesWithMaxBeauty(string s, int k) {\n \n }\n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "JavaScript",
|
|
"langSlug": "javascript",
|
|
"code": "/**\n * @param {string} s\n * @param {number} k\n * @return {number}\n */\nvar countKSubsequencesWithMaxBeauty = function(s, k) {\n \n};",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "TypeScript",
|
|
"langSlug": "typescript",
|
|
"code": "function countKSubsequencesWithMaxBeauty(s: string, k: number): number {\n\n};",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "PHP",
|
|
"langSlug": "php",
|
|
"code": "class Solution {\n\n /**\n * @param String $s\n * @param Integer $k\n * @return Integer\n */\n function countKSubsequencesWithMaxBeauty($s, $k) {\n \n }\n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Swift",
|
|
"langSlug": "swift",
|
|
"code": "class Solution {\n func countKSubsequencesWithMaxBeauty(_ s: String, _ k: Int) -> Int {\n \n }\n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Kotlin",
|
|
"langSlug": "kotlin",
|
|
"code": "class Solution {\n fun countKSubsequencesWithMaxBeauty(s: String, k: Int): Int {\n \n }\n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Dart",
|
|
"langSlug": "dart",
|
|
"code": "class Solution {\n int countKSubsequencesWithMaxBeauty(String s, int k) {\n\n }\n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Go",
|
|
"langSlug": "golang",
|
|
"code": "func countKSubsequencesWithMaxBeauty(s string, k int) int {\n \n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Ruby",
|
|
"langSlug": "ruby",
|
|
"code": "# @param {String} s\n# @param {Integer} k\n# @return {Integer}\ndef count_k_subsequences_with_max_beauty(s, k)\n \nend",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Scala",
|
|
"langSlug": "scala",
|
|
"code": "object Solution {\n def countKSubsequencesWithMaxBeauty(s: String, k: Int): Int = {\n \n }\n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Rust",
|
|
"langSlug": "rust",
|
|
"code": "impl Solution {\n pub fn count_k_subsequences_with_max_beauty(s: String, k: i32) -> i32 {\n \n }\n}",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Racket",
|
|
"langSlug": "racket",
|
|
"code": "(define/contract (count-k-subsequences-with-max-beauty s k)\n (-> string? exact-integer? exact-integer?)\n\n )",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Erlang",
|
|
"langSlug": "erlang",
|
|
"code": "-spec count_k_subsequences_with_max_beauty(S :: unicode:unicode_binary(), K :: integer()) -> integer().\ncount_k_subsequences_with_max_beauty(S, K) ->\n .",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "Elixir",
|
|
"langSlug": "elixir",
|
|
"code": "defmodule Solution do\n @spec count_k_subsequences_with_max_beauty(s :: String.t, k :: integer) :: integer\n def count_k_subsequences_with_max_beauty(s, k) do\n\n end\nend",
|
|
"__typename": "CodeSnippetNode"
|
|
}
|
|
],
|
|
"stats": "{\"totalAccepted\": \"9.5K\", \"totalSubmission\": \"33.8K\", \"totalAcceptedRaw\": 9490, \"totalSubmissionRaw\": 33795, \"acRate\": \"28.1%\"}",
|
|
"hints": [
|
|
"Since every character appears once in a k-subsequence, we can solve the following problem first: Find the total number of ways to select <code>k</code> characters such that the sum of their frequencies is maximum.",
|
|
"An obvious case to eliminate is if <code>k</code> is greater than the number of distinct characters in <code>s</code>, then the answer is <code>0</code>.",
|
|
"We are now interested in the top frequencies among the characters. Using a map data structure, let <code>cnt[x]</code> denote the number of characters that have a frequency of <code>x</code>.",
|
|
"Starting from the maximum value <code>x</code> in <code>cnt</code>. Let <code>i = min(k, cnt[x])</code> we add to our result <code> <sup>cnt[x]</sup>C<sub>i</sub> * x<sup>i</sup></code> representing the number of ways to select <code>i</code> characters from all characters with frequency <code>x</code>, multiplied by the number of ways to choose each individual character. Subtract <code>i</code> from <code>k</code> and continue downwards to the next maximum value.",
|
|
"Powers, combinations, and additions should be done modulo <code>10<sup>9</sup> + 7</code>."
|
|
],
|
|
"solution": null,
|
|
"status": null,
|
|
"sampleTestCase": "\"bcca\"\n2",
|
|
"metaData": "{\n \"name\": \"countKSubsequencesWithMaxBeauty\",\n \"params\": [\n {\n \"name\": \"s\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"k\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n }\n}",
|
|
"judgerAvailable": true,
|
|
"judgeType": "large",
|
|
"mysqlSchemas": [],
|
|
"enableRunCode": true,
|
|
"enableTestMode": false,
|
|
"enableDebugger": true,
|
|
"envInfo": "{\"cpp\": [\"C++\", \"<p>Compiled with <code> clang 11 </code> using the latest C++ 20 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\"], \"java\": [\"Java\", \"<p><code>OpenJDK 17</code>. Java 8 features such as lambda expressions and stream API can be used. </p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>\"], \"python\": [\"Python\", \"<p><code>Python 2.7.12</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/2/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/2/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/2/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\\r\\n\\r\\n<p>Note that Python 2.7 <a href=\\\"https://www.python.org/dev/peps/pep-0373/\\\" target=\\\"_blank\\\">will not be maintained past 2020</a>. For the latest Python, please choose Python3 instead.</p>\"], \"c\": [\"C\", \"<p>Compiled with <code>gcc 8.2</code> using the gnu11 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level one optimization (<code>-O1</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n\\r\\n<p>For hash table operations, you may use <a href=\\\"https://troydhanson.github.io/uthash/\\\" target=\\\"_blank\\\">uthash</a>. \\\"uthash.h\\\" is included by default. Below are some examples:</p>\\r\\n\\r\\n<p><b>1. Adding an item to a hash.</b>\\r\\n<pre>\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>2. Looking up an item in a hash:</b>\\r\\n<pre>\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>3. Deleting an item in a hash:</b>\\r\\n<pre>\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n</pre>\\r\\n</p>\"], \"csharp\": [\"C#\", \"<p><a href=\\\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10\\\" target=\\\"_blank\\\">C# 10 with .NET 6 runtime</a></p>\"], \"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"ruby\": [\"Ruby\", \"<p><code>Ruby 3.1</code></p>\\r\\n\\r\\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>\"], \"swift\": [\"Swift\", \"<p><code>Swift 5.5.2</code>.</p>\"], \"golang\": [\"Go\", \"<p><code>Go 1.21</code></p>\\r\\n<p>Support <a href=\\\"https://github.com/emirpasic/gods/tree/v1.18.1\\\" target=\\\"_blank\\\">https://godoc.org/github.com/emirpasic/gods@v1.18.1</a> library.</p>\"], \"python3\": [\"Python3\", \"<p><code>Python 3.10</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/3/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/3/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/3/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\"], \"scala\": [\"Scala\", \"<p><code>Scala 2.13.7</code>.</p>\"], \"kotlin\": [\"Kotlin\", \"<p><code>Kotlin 1.9.0</code>.</p>\"], \"rust\": [\"Rust\", \"<p><code>Rust 1.58.1</code></p>\\r\\n\\r\\n<p>Supports <a href=\\\"https://crates.io/crates/rand\\\" target=\\\"_blank\\\">rand </a> v0.6\\u00a0from crates.io</p>\"], \"php\": [\"PHP\", \"<p><code>PHP 8.1</code>.</p>\\r\\n<p>With bcmath module</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"], \"racket\": [\"Racket\", \"<p>Run with <code>Racket 8.3</code>.</p>\"], \"erlang\": [\"Erlang\", \"Erlang/OTP 25.0\"], \"elixir\": [\"Elixir\", \"Elixir 1.13.4 with Erlang/OTP 25.0\"], \"dart\": [\"Dart\", \"<p>Dart 2.17.3</p>\\r\\n\\r\\n<p>Your code will be run directly without compiling</p>\"]}",
|
|
"libraryUrl": null,
|
|
"adminUrl": null,
|
|
"challengeQuestion": null,
|
|
"__typename": "QuestionNode"
|
|
}
|
|
}
|
|
} |