Making File Names Unique Problem
Description
LeetCode Problem 1487.
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 have a suffix addition to its name in the form of (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:
Input: names = ["pes","fifa","gta","pes(2019)"]
Output: ["pes","fifa","gta","pes(2019)"]
Explanation: Let's see how the file system creates folder names:
"pes" --> not assigned before, remains "pes"
"fifa" --> not assigned before, remains "fifa"
"gta" --> not assigned before, remains "gta"
"pes(2019)" --> not assigned before, remains "pes(2019)"
Example 2:
Input: names = ["gta","gta(1)","gta","avalon"]
Output: ["gta","gta(1)","gta(2)","avalon"]
Explanation: Let's see how the file system creates folder names:
"gta" --> not assigned before, remains "gta"
"gta(1)" --> not assigned before, remains "gta(1)"
"gta" --> the name is reserved, system adds (k), since "gta(1)" is also reserved, systems put k = 2. it becomes "gta(2)"
"avalon" --> not assigned before, remains "avalon"
Example 3:
Input: names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"]
Output: ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"]
Explanation: When the last folder is created, the smallest positive valid k is 4, and it becomes "onepiece(4)".
Constraints:
1 <= names.length <= 5 * 10^41 <= names[i].length <= 20names[i]consists of lowercase English letters, digits, and/or round brackets.
Solution
Approach: Hash Map with the Next Suffix to Try
We process the names in order and keep a hash map whose keys are all the names already taken. For a name that is taken, the value is the next suffix k worth trying for it.
For each name:
- If it is not taken, use it as is and record it with next suffix
1. - If it is taken, start from its stored
kand increase it whilename(k)is taken. The first free one,name(k), is the folder's name. Recordname(k)as taken (next suffix1), and set the next suffix ofnametok + 1, because every suffix up tokis now known to be taken.
Remembering where we stopped is what keeps this fast. Without it, fifty thousand copies of "a" would each retry a(1), a(2), ... from the start, which is quadratic.
Note the names that already contain a suffix: in ["gta","gta(1)","gta","avalon"], "gta(1)" is taken by the second folder, so the third gets "gta(2)". A name like "gta(1)" is also an ordinary name of its own: if it repeats, it becomes "gta(1)(1)".
Complexity: O(L) expected time overall, where L is the total length of all names (each probe that fails moves the stored suffix forward for good), and O(L) extra space for the map.
class Solution:
def getFolderNames(self, names: List[str]) -> List[str]:
next_k = {} # every name in use -> the next suffix worth trying for it
ans = []
for name in names:
if name in next_k:
k = next_k[name]
while "%s(%d)" % (name, k) in next_k:
k += 1
next_k[name] = k + 1 # suffixes below k + 1 are all taken now
name = "%s(%d)" % (name, k)
next_k[name] = 1
ans.append(name)
return ans