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.

&nbsp;
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" --&gt; not assigned before, remains "pes"
"fifa" --&gt; not assigned before, remains "fifa"
"gta" --&gt; not assigned before, remains "gta"
"pes(2019)" --&gt; 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" --&gt; not assigned before, remains "gta"
"gta(1)" --&gt; not assigned before, remains "gta(1)"
"gta" --&gt; the name is reserved, system adds (k), since "gta(1)" is also reserved, systems put k = 2. it becomes "gta(2)"
"avalon" --&gt; 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)".


&nbsp;
Constraints:


	1 &lt;= names.length &lt;= 5 * 104
	1 &lt;= names[i].length &lt;= 20
	names[i] consists of lowercase English letters, digits, and/or round brackets.


class Solution:
    def getFolderNames(self, names: List[str]) -> List[str]:
        # Hashmap will store the name as key and the number of times that name has duplicated so fas as value.
        hashmap = {}

        for name in names:
            modified = name
            # Check whether the name has already been used
            if name in hashmap:
                # Get the number of times the {name} has been used
                k = hashmap[name]
                # Calculate the next available suffix.
                while modified in hashmap:
                    k += 1
                    modified = f'{name}({k})'
                # Update the number of times the original {name} is used. This will help to efficiently check for next available suffix if the {name} again comes in future.
                hashmap[name] = k
            # Store the modified {name} with 0 as it is not duplicated yet.
            hashmap[modified] = 0

        # Return the keys of hashmap as that would be the unique file names.
        return hashmap.keys()
class Solution {
    Map<String, Integer> map = new HashMap<>();
    
    public String[] getFolderNames(String[] names) {
        String[] op = new String[names.length];
        int i = 0;
        
        for(String cur : names){
            if(map.containsKey(cur)) {
                cur = generateCopyName(cur);
                op[i++] = cur;
             //   System.out.println(map.toString());
                continue;
            }
            
            op[i++] = cur;
            map.put(cur, 0);
           // System.out.println(map.toString());
        }
        
        return op;
    }
    
    private String generateCopyName(String s) {
        int count = map.get(s) + 1;
        
        String postfix = "(" + count + ")";
        StringBuilder sb = new StringBuilder(s);
        sb.append(postfix);
        
        boolean isChanged = false;
        while(map.containsKey(sb.toString())) {
            sb = new StringBuilder(s);
            sb.append("(" + count + ")");
            count++;
            isChanged = true;
        }
        String res = sb.toString();
        //System.out.println(res);
        
        
        if(isChanged)
            count = count -1;
        
        map.put(s, count);
        map.put(res, 0);
        
        return res;
    }
}
class Solution {
    
    unordered_map<string, int> next_index_;
    
    bool contain(const string& fn) {
        return next_index_.find(fn) != next_index_.end();
    }
    
    string get_name(string fn) {
        if (!contain(fn)) {
            next_index_.insert({fn, 1});
            return fn;
        }
        
        int idx = next_index_[fn];
        auto cur = fn;
        while (contain(cur)) {
            next_index_.insert({cur, 1});
            cur = fn + "(" + to_string(idx) + ")";
            ++idx;
        }
        next_index_.insert({cur, 1});
        next_index_[fn] = idx;
        return cur;
    }
    
public:
    vector<string> getFolderNames(vector<string>& names) {
        vector<string> res;
        for (auto& n : names) {
            res.push_back(get_name(n));
        }
        
        return res;
    }
};
/**
 * @param {string[]} names
 * @return {string[]}
 */
var getFolderNames = function(names) {
    // save existed folder names
    let hashMap = new Map();
    for(let name of names) {
        let finalName = name;
        // next smallest suffix number
        // if this name is not present in the map, next smallest suffix will be 1
        let number = hashMap.get(name) || 1;
        if(hashMap.has(name)) {
            // append suffix to original name
            finalName += '(' + number +')';
            // find the smallest suffix that hasn't been used before
            while(hashMap.has(finalName)) {
                number++;
                // try to use new suffix to update name
                finalName = name + '(' + number +')';
            }
            // update next smallest suffix number of new name
            hashMap.set(finalName, 1);
        }
        // update next smallest suffix number of original name
        hashMap.set(name, number);
    }
    return Array.from(hashMap.keys());
};
Making File Names Unique