{"id":8650,"date":"2021-11-01T18:35:52","date_gmt":"2021-11-02T01:35:52","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8650"},"modified":"2021-11-01T18:36:18","modified_gmt":"2021-11-02T01:36:18","slug":"leetcode-2053-kth-distinct-string-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2053-kth-distinct-string-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2053. Kth Distinct String in an Array"},"content":{"rendered":"\n<p>A&nbsp;<strong>distinct string<\/strong>&nbsp;is a string that is present only&nbsp;<strong>once<\/strong>&nbsp;in an array.<\/p>\n\n\n\n<p>Given an array of strings&nbsp;<code>arr<\/code>, and an integer&nbsp;<code>k<\/code>, return&nbsp;<em>the&nbsp;<\/em><code>k<sup>th<\/sup><\/code><em>&nbsp;<strong>distinct string<\/strong>&nbsp;present in&nbsp;<\/em><code>arr<\/code>. If there are&nbsp;<strong>fewer<\/strong>&nbsp;than&nbsp;<code>k<\/code>&nbsp;distinct strings, return&nbsp;<em>an&nbsp;<strong>empty string&nbsp;<\/strong><\/em><code>\"\"<\/code>.<\/p>\n\n\n\n<p>Note that the strings are considered in the&nbsp;<strong>order in which they appear<\/strong>&nbsp;in the array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [\"d\",\"b\",\"c\",\"b\",\"c\",\"a\"], k = 2\n<strong>Output:<\/strong> \"a\"\n<strong>Explanation:<\/strong>\nThe only distinct strings in arr are \"d\" and \"a\".\n\"d\" appears 1<sup>st<\/sup>, so it is the 1<sup>st<\/sup> distinct string.\n\"a\" appears 2<sup>nd<\/sup>, so it is the 2<sup>nd<\/sup> distinct string.\nSince k == 2, \"a\" is returned. \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [\"aaa\",\"aa\",\"a\"], k = 1\n<strong>Output:<\/strong> \"aaa\"\n<strong>Explanation:<\/strong>\nAll strings in arr are distinct, so the 1<sup>st<\/sup> string \"aaa\" is returned.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [\"a\",\"b\",\"a\"], k = 3\n<strong>Output:<\/strong> \"\"\n<strong>Explanation:<\/strong>\nThe only distinct string is \"b\". Since there are fewer than 3 distinct strings, we return an empty string \"\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= arr.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= arr[i].length &lt;= 5<\/code><\/li><li><code>arr[i]<\/code>&nbsp;consists of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string kthDistinct(vector<string>& arr, int k) {\n    unordered_map<string, int> m;\n    for (const string& s : arr)\n      ++m[s];    \n    for (const string& s : arr)\n      if (m[s] == 1 && --k == 0) return s;    \n    return \"\";\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;distinct string&nbsp;is a string that is present only&nbsp;once&nbsp;in an array. Given an array of strings&nbsp;arr, and an integer&nbsp;k, return&nbsp;the&nbsp;kth&nbsp;distinct string&nbsp;present in&nbsp;arr. If there are&nbsp;fewer&nbsp;than&nbsp;k&nbsp;distinct strings,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[20,222,82,4],"class_list":["post-8650","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-array","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8650"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8650\/revisions"}],"predecessor-version":[{"id":8652,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8650\/revisions\/8652"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}