1. Introduction
In this problem, we are given an array of usernames and need to generate unique usernames in the same order as the input.
If a username is available, we keep it unchanged. If it has already been assigned, we append the smallest positive integer suffix (1, 2, 3, ...) that creates a username that is not reserved or previously assigned.
The important condition is that all usernames in the original input are reserved from the beginning. Therefore, we must avoid generating a username that exists anywhere in the original array, even if that name hasn't been processed yet.
We can solve this problem efficiently using HashSet and string manipulation.
2. Problem example
Given:
arr = ["aba", "ab", "aba", "aba", "ab"]Expected output:
["aba", "ab", "aba1", "aba2", "ab1"]Explanation:
The first
"aba"is available, so it remains"aba".The first
"ab"is available, so it remains"ab".The second
"aba"is already assigned, so we generate"aba1".The third
"aba"is already assigned, so"aba1"is occupied and we generate"aba2".The second
"ab"is already assigned, so we generate"ab1".
3. C# solution
using System.Collections.Generic;
class Solution
{
public List<string> generateUsernames(string[] arr)
{
HashSet<string> used = new HashSet<string>(arr);
HashSet<string> assigned = new HashSet<string>();
List<string> result = new List<string>();
foreach (string name in arr)
{
if (!assigned.Contains(name))
{
assigned.Add(name);
result.Add(name);
}
else
{
int suffix = 1;
string candidate;
do
{
candidate = name + suffix;
suffix++;
}
while (used.Contains(candidate) || assigned.Contains(candidate));
assigned.Add(candidate);
used.Add(candidate);
result.Add(candidate);
}
}
return result;
}
}4. Core concept – HashSet
A HashSet is a collection that stores unique elements. It is useful when we need to quickly check whether a particular value already exists.
In this problem, we use two HashSets.
used
HashSet<string> used = new HashSet<string>(arr);This stores all original usernames and any newly generated usernames.
Purpose: Prevent generating a username that is already reserved or generated.
assigned
HashSet<string> assigned = new HashSet<string>();This stores usernames that have already been assigned to previous entries.
Purpose: Check whether the current original username has already been assigned.
result
List<string> result = new List<string>();This stores the final usernames in the same order as the input array.
Collection | Purpose |
|---|---|
| Reserved original names and generated names |
| Names already assigned to processed entries |
| Final output in input order |
5. Why do we reserve all usernames initially?
Consider this example:
arr = ["a", "a", "a1", "a"]At the beginning, the usernames "a" and "a1" are already reserved.
If we process the second "a" and generate "a1", we would create a username that belongs to an original entry in the input.
That is not allowed.
So, we initialize:
HashSet<string> used = new HashSet<string>(arr);Initially:
used = { "a", "a1" }This prevents us from accidentally generating "a1" before its original entry is processed.
6. Step-by-step dry run
Input
arr = ["a", "a", "a1", "a"]Step 1: Initialize collections
used = { "a", "a1" }
assigned = { }
result = [ ]All original names are reserved before processing.

Join the conversation! Your thoughts help the community grow.