Ctrl K

Unique Folder Names

Medium
Folders are created one by one, and requests[i] is the name asked for the i-th folder. No two folders may share a name. If the requested name is still free, the folder gets it. Otherwise the system appends a suffix (k) with the smallest integer k ≥ 1 that makes the name free. For example, if photo and photo(1) are taken, a new photo becomes photo(2). Return the names the folders actually receive, in creation order.
Example 1
requests=["notes", "draft", "notes", "notes(1)"] → ['notes', 'draft', 'notes(1)', 'notes(1)(1)']

The third "notes" is taken and becomes "notes(1)". The fourth asks for "notes(1)", which is now taken, so it becomes "notes(1)(1)".

Example 2
requests=["img", "img(2)", "img", "img"] → ['img', 'img(2)', 'img(1)', 'img(3)']

The second request for "img" gets "img(1)"; the third skips the taken "img(2)" and gets "img(3)".

Constraints
  • 1 ≤ len(requests) ≤ 5 · 10⁴
  • 1 ≤ len(requests[i]) ≤ 20
  • Names consist of lowercase English letters, digits and round brackets
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAnagram Buckets MediumSplit Into Dictionary Words MediumWords Equal Up to a Shift MediumA Subsequence of One Word Only MediumFiles With the Same Content Medium
loading Python…
Input
requests=["notes", "draft", "notes", "notes(1)"]
Expected
['notes', 'draft', 'notes(1)', 'notes(1)(1)']

Run checks the first 3 tests in your browser. Submit checks all 68 tests on the server, hidden ones included.