java.lang.Object
g1401_1500.s1487_making_file_names_unique.Solution

public class Solution extends Object
1487 - Making File Names Unique.<p>Medium</p> <p>Given an array of strings <code>names</code> of size <code>n</code>. You will create <code>n</code> folders in your file system <strong>such that</strong> , at the <code>i<sup>th</sup></code> minute, you will create a folder with the name <code>names[i]</code>.</p> <p>Since two files <strong>cannot</strong> have the same name, if you enter a folder name that was previously used, the system will have a suffix addition to its name in the form of <code>(k)</code>, where, <code>k</code> is the <strong>smallest positive integer</strong> such that the obtained name remains unique.</p> <p>Return <em>an array of strings of length</em> <code>n</code> where <code>ans[i]</code> is the actual name the system will assign to the <code>i<sup>th</sup></code> folder when you create it.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> names = [&ldquo;pes&rdquo;,&ldquo;fifa&rdquo;,&ldquo;gta&rdquo;,&ldquo;pes(2019)&rdquo;]</p> <p><strong>Output:</strong> [&ldquo;pes&rdquo;,&ldquo;fifa&rdquo;,&ldquo;gta&rdquo;,&ldquo;pes(2019)&rdquo;]</p> <p><strong>Explanation:</strong> Let&rsquo;s see how the file system creates folder names:</p> <p>&ldquo;pes&rdquo; &ndash;> not assigned before, remains &ldquo;pes&rdquo;</p> <p>&ldquo;fifa&rdquo; &ndash;> not assigned before, remains &ldquo;fifa&rdquo;</p> <p>&ldquo;gta&rdquo; &ndash;> not assigned before, remains &ldquo;gta&rdquo;</p> <p>&ldquo;pes(2019)&rdquo; &ndash;> not assigned before, remains &ldquo;pes(2019)&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> names = [&ldquo;gta&rdquo;,&ldquo;gta(1)&rdquo;,&ldquo;gta&rdquo;,&ldquo;avalon&rdquo;]</p> <p><strong>Output:</strong> [&ldquo;gta&rdquo;,&ldquo;gta(1)&rdquo;,&ldquo;gta(2)&rdquo;,&ldquo;avalon&rdquo;]</p> <p><strong>Explanation:</strong> Let&rsquo;s see how the file system creates folder names:</p> <p>&ldquo;gta&rdquo; &ndash;> not assigned before, remains &ldquo;gta&rdquo;</p> <p>&ldquo;gta(1)&rdquo; &ndash;> not assigned before, remains &ldquo;gta(1)&rdquo;</p> <p>&ldquo;gta&rdquo; &ndash;> the name is reserved, system adds (k), since &ldquo;gta(1)&rdquo; is also reserved, systems put k = 2. it becomes &ldquo;gta(2)&rdquo;</p> <p>&ldquo;avalon&rdquo; &ndash;> not assigned before, remains &ldquo;avalon&rdquo;</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> names = [&ldquo;onepiece&rdquo;,&ldquo;onepiece(1)&rdquo;,&ldquo;onepiece(2)&rdquo;,&ldquo;onepiece(3)&rdquo;,&ldquo;onepiece&rdquo;]</p> <p><strong>Output:</strong> [&ldquo;onepiece&rdquo;,&ldquo;onepiece(1)&rdquo;,&ldquo;onepiece(2)&rdquo;,&ldquo;onepiece(3)&rdquo;,&ldquo;onepiece(4)&rdquo;]</p> <p><strong>Explanation:</strong> When the last folder is created, the smallest positive valid k is 4, and it becomes &ldquo;onepiece(4)&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= names.length <= 5 * 10<sup>4</sup></code></li> <li><code>1 <= names[i].length <= 20</code></li> <li><code>names[i]</code> consists of lowercase English letters, digits, and/or round brackets.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • getFolderNames

      public String[] getFolderNames(String[] names)