Making File Names Unique
MediumUpdated: Aug 2, 2025
Practice on:
Problem
Given an array of strings names of size n. You will create n folders in your file system such that , at the ith minute, you will create a folder with the name names[i].
Since two files cannot have the same name, if you enter a folder name that was previously used, the system will have a suffix addition to its name in the form of (k), where, k is the smallest positive integer such that the obtained name remains unique.
Return an array of strings of lengthn where ans[i] is the actual name the system will assign to the ith folder when you create it.
Examples
Example 1
Input: names = ["pes","fifa","gta","pes(2019)"]
Output: ["pes","fifa","gta","pes(2019)"]
Explanation: Let's see how the file system creates folder names:
"pes" --> not assigned before, remains "pes"
"fifa" --> not assigned before, remains "fifa"
"gta" --> not assigned before, remains "gta"
"pes(2019)" --> not assigned before, remains "pes(2019)"
Example 2
Input: names = ["gta","gta(1)","gta","avalon"]
Output: ["gta","gta(1)","gta(2)","avalon"]
Explanation: Let's see how the file system creates folder names:
"gta" --> not assigned before, remains "gta"
"gta(1)" --> not assigned before, remains "gta(1)"
"gta" --> the name is reserved, system adds (k), since "gta(1)" is also reserved, systems put k = 2. it becomes "gta(2)"
"avalon" --> not assigned before, remains "avalon"
Example 3
Input: names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"]
Output: ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"]
Explanation: When the last folder is created, the smallest positive valid k is 4, and it becomes "onepiece(4)".
Constraints
1 <= names.length <= 5 * 10^41 <= names[i].length <= 20names[i]consists of lowercase English letters, digits, and/or round brackets.
Solution
Method 1 – Hash Map for Name Counting
Intuition
To ensure each file name is unique, we use a hash map to track the next available suffix for each base name. When a duplicate is found, we increment the suffix until a unique name is found.
Approach
- Use a hash map to store the next available integer suffix for each name.
- For each name in the input:
- If the name is not in the map, add it to the answer and set its suffix to 1.
- If the name is already used, increment the suffix until a unique name is found, then add it to the answer and update the map.
- Return the answer array.
Code
C++
class Solution {
public:
vector<string> getFolderNames(vector<string>& names) {
unordered_map<string, int> mp;
vector<string> ans;
for (auto& name : names) {
if (!mp.count(name)) {
ans.push_back(name);
mp[name] = 1;
} else {
int k = mp[name];
string newName;
while (true) {
newName = name + "(" + to_string(k) + ")";
if (!mp.count(newName)) break;
k++;
}
ans.push_back(newName);
mp[name] = k + 1;
mp[newName] = 1;
}
}
return ans;
}
};
Go
func getFolderNames(names []string) []string {
mp := make(map[string]int)
ans := make([]string, 0, len(names))
for _, name := range names {
if _, ok := mp[name]; !ok {
ans = append(ans, name)
mp[name] = 1
} else {
k := mp[name]
var newName string
for {
newName = name + "(" + strconv.Itoa(k) + ")"
if _, ok := mp[newName]; !ok {
break
}
k++
}
ans = append(ans, newName)
mp[name] = k + 1
mp[newName] = 1
}
}
return ans
}
Java
class Solution {
public String[] getFolderNames(String[] names) {
Map<String, Integer> mp = new HashMap<>();
String[] ans = new String[names.length];
for (int i = 0; i < names.length; i++) {
String name = names[i];
if (!mp.containsKey(name)) {
ans[i] = name;
mp.put(name, 1);
} else {
int k = mp.get(name);
String newName;
while (true) {
newName = name + "(" + k + ")";
if (!mp.containsKey(newName)) break;
k++;
}
ans[i] = newName;
mp.put(name, k + 1);
mp.put(newName, 1);
}
}
return ans;
}
}
Kotlin
class Solution {
fun getFolderNames(names: Array<String>): Array<String> {
val mp = mutableMapOf<String, Int>()
val ans = Array(names.size) { "" }
for (i in names.indices) {
val name = names[i]
if (name !in mp) {
ans[i] = name
mp[name] = 1
} else {
var k = mp[name]!!
var newName: String
while (true) {
newName = "$name($k)"
if (newName !in mp) break
k++
}
ans[i] = newName
mp[name] = k + 1
mp[newName] = 1
}
}
return ans
}
}
Python
class Solution:
def getFolderNames(self, names: list[str]) -> list[str]:
mp: dict[str, int] = {}
ans: list[str] = []
for name in names:
if name not in mp:
ans.append(name)
mp[name] = 1
else:
k = mp[name]
while True:
new_name = f"{name}({k})"
if new_name not in mp:
break
k += 1
ans.append(new_name)
mp[name] = k + 1
mp[new_name] = 1
return ans
Rust
impl Solution {
pub fn get_folder_names(names: Vec<String>) -> Vec<String> {
use std::collections::HashMap;
let mut mp = HashMap::new();
let mut ans = Vec::with_capacity(names.len());
for name in names.iter() {
if !mp.contains_key(name) {
ans.push(name.clone());
mp.insert(name.clone(), 1);
} else {
let mut k = *mp.get(name).unwrap();
loop {
let new_name = format!("{}({})", name, k);
if !mp.contains_key(&new_name) {
ans.push(new_name.clone());
mp.insert(name.clone(), k + 1);
mp.insert(new_name, 1);
break;
}
k += 1;
}
}
}
ans
}
}
TypeScript
class Solution {
getFolderNames(names: string[]): string[] {
const mp = new Map<string, number>();
const ans: string[] = [];
for (const name of names) {
if (!mp.has(name)) {
ans.push(name);
mp.set(name, 1);
} else {
let k = mp.get(name)!;
let newName = '';
while (true) {
newName = `${name}(${k})`;
if (!mp.has(newName)) break;
k++;
}
ans.push(newName);
mp.set(name, k + 1);
mp.set(newName, 1);
}
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n * m), wherenis the number of names andmis the average length of the names, since each name and its suffixes are checked for uniqueness. - 🧺 Space complexity:
O(n * m), for storing all unique names in the hash map.