class FindElements(object):
def getFolderNames(self, names):
"""
:type names: List[str]
:rtype: List[str]
"""
mydict = {}
res = []
for item in names:
if item in mydict.keys():
mydict[item] += 1
newName = item + "(" + str(mydict[item]) + ")"
while newName in mydict.keys():
mydict[item] += 1
newName = item + "(" + str(mydict[item]) + ")"
res.append(newName)
mydict[newName] = 0
else:
mydict[item] = 0
res.append(item)
return res
代码二:python3
class Solution(object):
def getFolderNames(self, names):
mydict, res = {}, []
for name in names:
temp = name
while temp in mydict:
temp = f'{name}({mydict[name]})'
mydict[name] += 1
mydict[temp] = 1
res.append(temp)
return res