Making File Names Unique LeetCode solution in Python

1
# Runtime: 1004 ms (Top 5.08%) | Memory: 28.1 MB (Top 35.29%)
2
class Solution:
3
def getFolderNames(self, names: List[str]) -> List[str]:
4
# Hashmap will store the name as key and the number of times that name has duplicated so fas as value.
5
hashmap = {}
6

7
for name in names:
8
modified = name
9
# Check whether the name has already been used
10
if name in hashmap:
11
# Get the number of times the {name} has been used
12
k = hashmap[name]
13
# Calculate the next available suffix.
14
while modified in hashmap:
15
k += 1
16
modified = f"{name}({k})"
17
# 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.
18
hashmap[name] = k
19
# Store the modified {name} with 0 as it is not duplicated yet.
20
hashmap[modified] = 0
21

22
# Return the keys of hashmap as that would be the unique file names.
23
return hashmap.keys()

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0