<p>Given an array of strings <code>names</code> of size <code>n</code>. You will create <code>n</code> folders in your file system <strong>such that</strong>, at the <code>i<sup>th</sup></code> minute, you will create a folder with the name <code>names[i]</code>.</p>

<p>Since two files <strong>cannot</strong> 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 <code>(k)</code>, where, <code>k</code> is the <strong>smallest positive integer</strong> such that the obtained name remains unique.</p>

<p>Return <em>an array of strings of length </em><code>n</code> where <code>ans[i]</code> is the actual name the system will assign to the <code>i<sup>th</sup></code> folder when you create it.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> names = [&quot;pes&quot;,&quot;fifa&quot;,&quot;gta&quot;,&quot;pes(2019)&quot;]
<strong>Output:</strong> [&quot;pes&quot;,&quot;fifa&quot;,&quot;gta&quot;,&quot;pes(2019)&quot;]
<strong>Explanation:</strong> Let&#39;s see how the file system creates folder names:
&quot;pes&quot; --&gt; not assigned before, remains &quot;pes&quot;
&quot;fifa&quot; --&gt; not assigned before, remains &quot;fifa&quot;
&quot;gta&quot; --&gt; not assigned before, remains &quot;gta&quot;
&quot;pes(2019)&quot; --&gt; not assigned before, remains &quot;pes(2019)&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> names = [&quot;gta&quot;,&quot;gta(1)&quot;,&quot;gta&quot;,&quot;avalon&quot;]
<strong>Output:</strong> [&quot;gta&quot;,&quot;gta(1)&quot;,&quot;gta(2)&quot;,&quot;avalon&quot;]
<strong>Explanation:</strong> Let&#39;s see how the file system creates folder names:
&quot;gta&quot; --&gt; not assigned before, remains &quot;gta&quot;
&quot;gta(1)&quot; --&gt; not assigned before, remains &quot;gta(1)&quot;
&quot;gta&quot; --&gt; the name is reserved, system adds (k), since &quot;gta(1)&quot; is also reserved, systems put k = 2. it becomes &quot;gta(2)&quot;
&quot;avalon&quot; --&gt; not assigned before, remains &quot;avalon&quot;
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> names = [&quot;onepiece&quot;,&quot;onepiece(1)&quot;,&quot;onepiece(2)&quot;,&quot;onepiece(3)&quot;,&quot;onepiece&quot;]
<strong>Output:</strong> [&quot;onepiece&quot;,&quot;onepiece(1)&quot;,&quot;onepiece(2)&quot;,&quot;onepiece(3)&quot;,&quot;onepiece(4)&quot;]
<strong>Explanation:</strong> When the last folder is created, the smallest positive valid k is 4, and it becomes &quot;onepiece(4)&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= names.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
	<li><code>names[i]</code> consists of lowercase English letters, digits, and/or round brackets.</li>
</ul>

class Solution:
    def getFolderNames(self, names: List[str]) -> List[str]:
        cache = {}#store names

        for idx, name in enumerate(names):
            modified = name
            if name not in cache:
                #while(name)
                cache[name] = 0
            else:
                k = cache[name]
                while modified in cache:
                    k += 1
                    modified = f'{name}({k})'
                cache[name] = k
                cache[modified] = 0
        return cache.keys()
        #last: pes:0, pes(2):0, pes(1):0, pes(3):0



        #["pes","pes(2)", "pes", "pes"]
#expected["pes","pes(2)", "pes(1)","pes(3)" ]
        #last: pes:3, pes(2):0, pes(1):0, pes(3):0
        #modified = pes(3)
        '''
        cache = {}#store names
        res = []

        for idx, i in enumea
        

        
        for i in names:
            #not found right away, then
            if i not in cache:
                if '(' in i and ')' in i:
                    for name in cache: #break this for loop
                        
                        if name in i:
                            leng = len(name)
                            if len(i) == leng + 3 and i[leng] == "(" and i[leng+2] == ")":
                                #check if it is repeaded :
                                if i in cache[name]:
                                    break #do nothing
                                #check if i_k in within k:
                                    
                                    
                                    i_k = leng+1
                                    if i_k > k:
                                        cache[name][count] += 1
                                        k = cache[name][count]
                                        st = name + "(" + str(k) + ")"
                                        res.append(st)
                                        cache[name][i] = 1
                                    
                                    
                                cache[name][count] += 1
                                k = cache[name][count]
                                st = name + "(" + str(k) + ")"
                                res.append(st)
                                break
                res.append(i)
                cache[i] = {}
                cache[i][count] = 0

            else:
                
                cache[i][count] += 1
                #res.append(i)
                k = cache[i][count]
                #cache
                st = i + "(" + str(k) + ")"
                res.append(st)
                cache[i][st] = 1

        return res 


        ["pes","fifa","gta","pes(1)", "pes(2019)"]
        '''
29700