1487. Making File Names Unique
UnknownView on LeetCode
Time: O(n·m)
Space: O(n·m)
Problem Overview
Making File Names Unique (Unknown) asks you to solve a structured algorithmic task. This is a common Hash Table / String pattern in coding interviews. Dictionary tracks the next suffix to try for each name; increment suffix until a fresh name is found.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Dictionary tracks the next suffix to try for each name; increment suffix until a fresh name is found.
Related patterns: Hash Table, String
1487.cs
C#
// Approach: Dictionary tracks the next suffix to try for each name; increment suffix until a fresh name is found.
// Time: O(n·m) Space: O(n·m)
public class Solution
{
public string[] GetFolderNames(string[] names)
{
string[] ans = new string[names.Length];
Dictionary<string, int> nameToSuffix = new Dictionary<string, int>();
for (int i = 0; i < names.Length; ++i)
{
string name = names[i];
if (nameToSuffix.ContainsKey(name))
{
int suffix = nameToSuffix[name];
string newName = GetName(name, ++suffix);
while (nameToSuffix.ContainsKey(newName))
newName = GetName(name, ++suffix);
nameToSuffix[name] = suffix;
nameToSuffix[newName] = 0;
ans[i] = newName;
}
else
{
nameToSuffix[name] = 0;
ans[i] = name;
}
}
return ans;
}
private string GetName(string name, int suffix)
{
return name + "(" + suffix.ToString() + ")";
}
}Was this solution helpful?