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 lengthn where ans[i] is the actual name the system will assign to the ith folder when you create it.
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)"
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"
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 is4, and it becomes "onepiece(4)".
To ensure each file name is unique, we use a hash map to track the next available suffix for each base name. When a duplicate is found, we increment the suffix until a unique name is found.
⏰ Time complexity: O(n * m), where n is the number of names and m is the average length of the names, since each name and its suffixes are checked for uniqueness.
🧺 Space complexity: O(n * m), for storing all unique names in the hash map.