Given an array of strings names
of size n
. You will create n
folders in your file system such that, at the ith
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 ith
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 * 104
1 <= names[i].length <= 20
names[i]
consists of lowercase English letters, digits, and/or round brackets.This problem requires creating unique folder names, adding suffixes like (k)
when a name already exists. The solutions use a hash map (dictionary in Python) to efficiently track used names and their suffix counts.
Approach:
Initialization: A hash map (e.g., d
in the code) stores folder names as keys and their suffix counts (or 1 if not suffixed) as values.
Iteration: The code iterates through the input names
array.
Uniqueness Check: For each name, it checks if the name already exists in the hash map.
Suffix Generation: If a name collision occurs:
k
) from the hash map is retrieved.while
loop finds the smallest available k
such that name(k)
isn't already used.(k)
.k+1
is stored back in the hash map.Hash Map Update: After processing each name (whether it was modified or not), its updated name is added/updated in the hash map with a count of 1 (or updated count if it already existed).
Return: The modified names
array is returned.
Time Complexity: O(N*M), where N is the length of the input array names
, and M is the maximum length of a name string. In the worst-case scenario, many name collisions could occur, requiring multiple iterations within the while loop to find a unique suffix. However, the average-case complexity is much closer to O(N) because collisions are relatively rare.
Space Complexity: O(N), as the hash map could potentially store all the unique names generated. In the worst-case scenario (all names are the same), the hashmap would only store the original name and its updated suffix versions.
Example Walkthrough (Java):
Let's trace Example 2: names = ["gta","gta(1)","gta","avalon"]
d
is initialized as an empty HashMap.d
doesn't contain "gta", so it's added: d = {"gta": 1}
.d
doesn't contain "gta(1)", so it's added: d = {"gta": 1, "gta(1)": 1}
.d
contains "gta", so k
becomes 1. The while loop checks for "gta(1)", which exists, so k
becomes 2. "gta(2)" is added: names[2]
becomes "gta(2)", and d
updates to d = {"gta": 2, "gta(1)": 1, "gta(2)": 1}
.d
doesn't contain "avalon", so it's added: d = {"gta": 2, "gta(1)": 1, "gta(2)": 1, "avalon": 1}
.names
array ["gta","gta(1)","gta(2)","avalon"]
is returned.The other languages (Python, C++, Go, TypeScript) follow the same logic, differing only in syntax and data structure implementation.