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 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
๐Ÿ—‚๏ธ Smart File System Name Resolution๐Ÿ“‹ Name Registrydocdoc(1)imagedoc(1)(1)doc(2)๐Ÿ”ข Suffix Countersdoc โ†’ 3image โ†’ 1doc(1) โ†’ 2file โ†’ 1Next available suffixโšก ProcessingInput: "doc"Registry: โœ“ FoundCounter: doc โ†’ 3Try: "doc(3)"Registry: โœ— AvailableResult: "doc(3)"Update: doc โ†’ 4๐ŸŽฏ Algorithm Flow1. Check if name exists in registry (O(1))2. If unique โ†’ add directly, if duplicate โ†’ get next suffix from counter3. Update registry and increment counter for future conflicts๐Ÿ“Š Complexity AnalysisTime: O(n) - Each name processed once with O(1) hash operationsSpace: O(n) - Hash set and map store at most n unique entriesvs Brute Force O(nยฒ) time - eliminates redundant linear searches!
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.
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
28.6K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen