{"id":6956,"date":"2020-06-20T22:31:46","date_gmt":"2020-06-21T05:31:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6956"},"modified":"2020-06-20T22:33:08","modified_gmt":"2020-06-21T05:33:08","slug":"leetcode-1487-making-file-names-unique","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1487-making-file-names-unique\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1487. Making File Names Unique"},"content":{"rendered":"\n<p>Given an array of strings&nbsp;<code>names<\/code>&nbsp;of size&nbsp;<code>n<\/code>. You will create&nbsp;<code>n<\/code>&nbsp;folders in your file system&nbsp;<strong>such that<\/strong>, at the&nbsp;<code>ith<\/code>&nbsp;minute, you will create a folder with the name&nbsp;<code>names[i]<\/code>.<\/p>\n\n\n\n<p>Since two files&nbsp;<strong>cannot<\/strong>&nbsp;have the same name, if you enter a folder name which is previously used,&nbsp;the system&nbsp;will have a suffix&nbsp;addition to its name in the form of&nbsp;<code>(k)<\/code>,&nbsp;where,&nbsp;<code>k<\/code>&nbsp;is the&nbsp;<strong>smallest positive integer<\/strong>&nbsp;such that the obtained name remains unique.<\/p>\n\n\n\n<p>Return&nbsp;<em>an array of strings of length&nbsp;<code>n<\/code><\/em>&nbsp;where&nbsp;<code>ans[i]<\/code>&nbsp;is the actual name the system will assign to the&nbsp;<code>ith<\/code>&nbsp;folder when you create it.<\/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> names = [\"pes\",\"fifa\",\"gta\",\"pes(2019)\"]\n<strong>Output:<\/strong> [\"pes\",\"fifa\",\"gta\",\"pes(2019)\"]\n<strong>Explanation:<\/strong> Let's see how the file system creates folder names:\n\"pes\" --&gt; not assigned before, remains \"pes\"\n\"fifa\" --&gt; not assigned before, remains \"fifa\"\n\"gta\" --&gt; not assigned before, remains \"gta\"\n\"pes(2019)\" --&gt; not assigned before, remains \"pes(2019)\"\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> names = [\"gta\",\"gta(1)\",\"gta\",\"avalon\"]\n<strong>Output:<\/strong> [\"gta\",\"gta(1)\",\"gta(2)\",\"avalon\"]\n<strong>Explanation:<\/strong> Let's see how the file system creates folder names:\n\"gta\" --&gt; not assigned before, remains \"gta\"\n\"gta(1)\" --&gt; not assigned before, remains \"gta(1)\"\n\"gta\" --&gt; the name is reserved, system adds (k), since \"gta(1)\" is also reserved, systems put k = 2. it becomes \"gta(2)\"\n\"avalon\" --&gt; not assigned before, remains \"avalon\"\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> names = [\"onepiece\",\"onepiece(1)\",\"onepiece(2)\",\"onepiece(3)\",\"onepiece\"]\n<strong>Output:<\/strong> [\"onepiece\",\"onepiece(1)\",\"onepiece(2)\",\"onepiece(3)\",\"onepiece(4)\"]\n<strong>Explanation:<\/strong> When the last folder is created, the smallest positive valid k is 4, and it becomes \"onepiece(4)\".\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> names = [\"wano\",\"wano\",\"wano\",\"wano\"]\n<strong>Output:<\/strong> [\"wano\",\"wano(1)\",\"wano(2)\",\"wano(3)\"]\n<strong>Explanation:<\/strong> Just increase the value of k each time you create folder \"wano\".\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> names = [\"kaido\",\"kaido(1)\",\"kaido\",\"kaido(1)\"]\n<strong>Output:<\/strong> [\"kaido\",\"kaido(1)\",\"kaido(2)\",\"kaido(1)(1)\"]\n<strong>Explanation:<\/strong> Please note that system adds the suffix (k) to current name even it contained the same suffix before.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= names.length &lt;= 5 * 10^4<\/code><\/li><li><code>1 &lt;= names[i].length &lt;= 20<\/code><\/li><li><code>names[i]<\/code>&nbsp;consists of lower case English letters, digits and\/or round brackets.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Use a hashtable to store the mapping form base_name to its next suffix index.<\/p>\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  vector<string> getFolderNames(vector<string>& names) {\n    vector<string> ans;\n    unordered_map<string, int> m; \/\/ base_name -> next suffix.\n    for (const string& name : names) {\n      string unique_name = name;\n      int j = m[name];\n      if (j > 0) {\n        while (m.count(unique_name = name + \"(\" + to_string(j++) + \")\"));    \n        m[name] = j;        \n      }\n      m[unique_name] = 1;\n      ans.push_back(unique_name);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of strings&nbsp;names&nbsp;of size&nbsp;n. You will create&nbsp;n&nbsp;folders in your file system&nbsp;such that, at the&nbsp;ith&nbsp;minute, you will create a folder with the name&nbsp;names[i]. Since&#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":[82,4],"class_list":["post-6956","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6956","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=6956"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6956\/revisions"}],"predecessor-version":[{"id":6958,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6956\/revisions\/6958"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}