Making File Names Unique - Problem
Unique File System Manager
You're building a file system that automatically handles duplicate folder names! Given an array of
Here's the challenge: no two folders can have identical names. When you try to create a folder with a name that already exists, the system automatically appends a suffix
Goal: Return an array showing the actual names assigned to each folder after the system processes duplicates.
Example: If you try to create folders [
You're building a file system that automatically handles duplicate folder names! Given an array of
names containing n folder names, you need to create folders one by one in chronological order.Here's the challenge: no two folders can have identical names. When you try to create a folder with a name that already exists, the system automatically appends a suffix
(k) where k is the smallest positive integer that makes the name unique.Goal: Return an array showing the actual names assigned to each folder after the system processes duplicates.
Example: If you try to create folders [
"doc", "doc", "doc"], the system will create: ["doc", "doc(1)", "doc(2)"] Input & Output
example_1.py โ Basic duplicates
$
Input:
["doc", "doc", "image", "doc(1)", "doc"]
โบ
Output:
["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"]
๐ก Note:
First 'doc' is unique. Second 'doc' becomes 'doc(1)'. 'image' is unique. 'doc(1)' already exists, so it becomes 'doc(1)(1)'. Last 'doc' becomes 'doc(2)' since 'doc' and 'doc(1)' exist.
example_2.py โ No duplicates
$
Input:
["file", "image", "video"]
โบ
Output:
["file", "image", "video"]
๐ก Note:
All names are unique, so no modifications needed. The output is identical to input.
example_3.py โ Edge case with parentheses
$
Input:
["test", "test(1)", "test", "test(1)", "test(2)", "test"]
โบ
Output:
["test", "test(1)", "test(2)", "test(1)(1)", "test(2)(1)", "test(3)"]
๐ก Note:
Complex case where both base names and names with suffixes can conflict. Each gets the smallest available suffix.
Constraints
- 1 โค names.length โค 5 ร 104
- 1 โค names[i].length โค 20
- names[i] consists of lowercase English letters, digits, and/or round brackets
- No guarantee on input format - names may already contain parentheses
Visualization
Tap to expand
Understanding the Visualization
1
Registry Setup
Initialize a registry to track all used names and counters for each base name
2
Name Processing
For each incoming folder request, check the registry instantly
3
Conflict Resolution
If conflict found, use the counter to assign next available suffix
4
Registry Update
Update both the used names set and increment the counter for future conflicts
Key Takeaway
๐ฏ Key Insight: By maintaining a counter for each base name, we eliminate the need to search from suffix (1) every time, transforming an O(nยฒ) problem into an optimal O(n) solution.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code