Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 length n where ans[i] is the actual name the system will assign to the ith folder when you create it.
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 * 1041 <= names[i].length <= 20names[i] consists of lowercase English letters, digits, and/or round brackets.Problem summary: 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 length n where ans[i] is the actual name the system will assign to the ith folder when you create it.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["pes","fifa","gta","pes(2019)"]
["gta","gta(1)","gta","avalon"]
["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1487: Making File Names Unique
class Solution {
public String[] getFolderNames(String[] names) {
Map<String, Integer> d = new HashMap<>();
for (int i = 0; i < names.length; ++i) {
if (d.containsKey(names[i])) {
int k = d.get(names[i]);
while (d.containsKey(names[i] + "(" + k + ")")) {
++k;
}
d.put(names[i], k);
names[i] += "(" + k + ")";
}
d.put(names[i], 1);
}
return names;
}
}
// Accepted solution for LeetCode #1487: Making File Names Unique
func getFolderNames(names []string) []string {
d := map[string]int{}
for i, name := range names {
if k, ok := d[name]; ok {
for {
newName := fmt.Sprintf("%s(%d)", name, k)
if d[newName] == 0 {
d[name] = k + 1
names[i] = newName
break
}
k++
}
}
d[names[i]] = 1
}
return names
}
# Accepted solution for LeetCode #1487: Making File Names Unique
class Solution:
def getFolderNames(self, names: List[str]) -> List[str]:
d = defaultdict(int)
for i, name in enumerate(names):
if name in d:
k = d[name]
while f'{name}({k})' in d:
k += 1
d[name] = k + 1
names[i] = f'{name}({k})'
d[names[i]] = 1
return names
// Accepted solution for LeetCode #1487: Making File Names Unique
struct Solution;
use std::collections::HashMap;
use std::collections::HashSet;
impl Solution {
fn get_folder_names(names: Vec<String>) -> Vec<String> {
let mut hs: HashSet<String> = HashSet::new();
let mut hm: HashMap<String, usize> = HashMap::new();
let mut res = vec![];
'outer: for name in names {
if !hs.insert(name.to_string()) {
let mut i = *hm.get(&name).unwrap_or(&1);
loop {
let new_name = format!("{}({})", name, i);
if hs.insert(new_name.to_string()) {
res.push(new_name);
hm.insert(name, i + 1);
continue 'outer;
}
i += 1;
}
} else {
res.push(name);
}
}
res
}
}
#[test]
fn test() {
let names = vec_string!["pes", "fifa", "gta", "pes(2019)"];
let res = vec_string!["pes", "fifa", "gta", "pes(2019)"];
assert_eq!(Solution::get_folder_names(names), res);
let names = vec_string!["gta", "gta(1)", "gta", "avalon"];
let res = vec_string!["gta", "gta(1)", "gta(2)", "avalon"];
assert_eq!(Solution::get_folder_names(names), res);
let names = vec_string![
"onepiece",
"onepiece(1)",
"onepiece(2)",
"onepiece(3)",
"onepiece"
];
let res = vec_string![
"onepiece",
"onepiece(1)",
"onepiece(2)",
"onepiece(3)",
"onepiece(4)"
];
assert_eq!(Solution::get_folder_names(names), res);
let names = vec_string!["wano", "wano", "wano", "wano"];
let res = vec_string!["wano", "wano(1)", "wano(2)", "wano(3)"];
assert_eq!(Solution::get_folder_names(names), res);
let names = vec_string!["kaido", "kaido(1)", "kaido", "kaido(1)"];
let res = vec_string!["kaido", "kaido(1)", "kaido(2)", "kaido(1)(1)"];
assert_eq!(Solution::get_folder_names(names), res);
}
// Accepted solution for LeetCode #1487: Making File Names Unique
function getFolderNames(names: string[]): string[] {
let d: Map<string, number> = new Map();
for (let i = 0; i < names.length; ++i) {
if (d.has(names[i])) {
let k: number = d.get(names[i]) || 0;
while (d.has(names[i] + '(' + k + ')')) {
++k;
}
d.set(names[i], k);
names[i] += '(' + k + ')';
}
d.set(names[i], 1);
}
return names;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.