#1487
Making File Names Unique
MediumArrayHash TableString
Approaches
💡
Intuition
Time Space
In the brute force approach, we will check each folder name against all previously created names to ensure uniqueness. If a name already exists, we will keep appending a suffix until we find a unique name.
⚙️
Algorithm
5 steps- 1Step 1: Initialize an empty list 'result' to store the final folder names.
- 2Step 2: For each name in the input list, check if it exists in 'result'.
- 3Step 3: If it exists, append '(k)' where k is the smallest integer that makes the name unique, and keep checking until a unique name is found.
- 4Step 4: If the name is unique, add it directly to 'result'.
- 5Step 5: Return the 'result' list.
solution.py13 lines
1def getFolderNames(names):
2 result = []
3 for name in names:
4 if name not in result:
5 result.append(name)
6 else:
7 k = 1
8 new_name = f'{name}({k})'
9 while new_name in result:
10 k += 1
11 new_name = f'{name}({k})'
12 result.append(new_name)
13 return resultSolutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.