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.
Step 2: Process the first "a"
The username "a" has not been assigned yet.
if (!assigned.Contains(name))The condition is true.
We add "a" to assigned and result.
assigned = { "a" }
result = [ "a" ]Step 3: Process the second "a"
The username "a" is already assigned.
We start searching for a suffix:
a1But "a1" is reserved in used.
So, we continue:
a2"a2" is available.
We add it to assigned, used, and result.
assigned = { "a", "a2" }
used = { "a", "a1", "a2" }
result = [ "a", "a2" ]Step 4: Process "a1"
The original username "a1" has not yet been assigned.
Although it is already in used, it is not in assigned.
Therefore, we keep it unchanged.
assigned = { "a", "a2", "a1" }
result = [ "a", "a2", "a1" ]Step 5: Process the final "a"
The name "a" has already been assigned.
We check:
Candidate | Status |
|---|---|
| Reserved / assigned |
| Already generated |
| Available |
We assign "a3".
Final result:
["a", "a2", "a1", "a3"]7. Understanding the suffix-generation loop
This is the most important part of the solution.
int suffix = 1;
string candidate;
do
{
candidate = name + suffix;
suffix++;
}
while (used.Contains(candidate) || assigned.Contains(candidate));How it works
Suppose the original name is "aba".
We try:
aba1
aba2
aba3
aba4
...For each candidate, we check:
used.Contains(candidate) || assigned.Contains(candidate)If the candidate exists in
used, it is reserved or generated.If the candidate exists in
assigned, it has already been assigned.
If either condition is true, we try the next suffix.
The loop stops when we find the smallest available username.
Why use do-while?
A do-while loop executes at least once. We need to generate the first candidate (name + 1) before checking whether it is available.
For example:
aba1If it is occupied, the loop continues with:
aba2If that is also occupied, it tries:
aba3And so on.
8. Line-by-line code explanation
Step 1: Create the reserved username set
HashSet<string> used = new HashSet<string>(arr);Copies all original usernames into a HashSet.
Prevents generated usernames from conflicting with any original username.
Step 2: Create the assigned set
HashSet<string> assigned = new HashSet<string>();Tracks usernames already given to previous entries.
Helps distinguish between a reserved username and one already assigned.
Step 3: Create the result list
List<string> result = new List<string>();Stores the generated usernames in the original input order.
Step 4: Iterate through the input
foreach (string name in arr)We process each name sequentially.
Step 5: Keep the original name if available
if (!assigned.Contains(name))
{
assigned.Add(name);
result.Add(name);
}If the name hasn't already been assigned, we keep it unchanged and add it to our collections.
Step 6: Generate a new username for duplicates
else
{
int suffix = 1;
string candidate;If the name is already assigned, we start generating suffixes from 1.
Step 7: Search for the smallest available username
do
{
candidate = name + suffix;
suffix++;
}
while (used.Contains(candidate) || assigned.Contains(candidate));We keep generating candidates until we find one that is neither reserved nor already assigned.
Step 8: Store the generated username
assigned.Add(candidate);
used.Add(candidate);
result.Add(candidate);Once we find the candidate:
Add it to
assigned.Add it to
used.Add it to
result.
Step 9: Return the output
return result;Returns all generated usernames in the same order as the input.
9. Complexity analysis
Let:
n= number of usernames.L= maximum length of a username.
Complexity | Explanation |
|---|---|
Time: O(n² × L) | In the worst case, we may search through many suffixes for each duplicate, and string operations involve the username length. |
Auxiliary Space: O(n × L) | We store original usernames, generated usernames, assigned usernames and the result. |


Join the conversation! Your thoughts help the community grow.