Making File Names Unique

Given an array of strings names of size n, you will create n folders in your file system such that, at the i^th minute, you will create a folder with the name names[i].

Since two files cannot have the same name, if you enter a folder name that was previously used, the system will add a suffix to its name in the form (k), where k is the smallest positive integer such that the obtained name remains unique.

Return an array of strings of length n where ans[i] is the actual name the system will assign to the i^th folder when you create it.

Example 1
Inputnames = ["pes","fifa","gta","pes(2019)"]
Output["pes","fifa","gta","pes(2019)"]
Each folder name has not been assigned before, so every name remains unchanged.
Example 2
Inputnames = ["gta","gta(1)","gta","avalon"]
Output["gta","gta(1)","gta(2)","avalon"]
The second "gta" is already reserved, and since "gta(1)" is also reserved, the smallest valid suffix is 2.

Constraints

  • 1 <= names.length <= 5 * 10^4
  • 1 <= names[i].length <= 20
  • names[i] consists of lowercase English letters, digits, and/or round brackets.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate