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:

Code
arr = ["aba", "ab", "aba", "aba", "ab"]

Expected output:

CSS
["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

C#
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

C#
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

C#
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

C#
List<string> result = new List<string>();

This stores the final usernames in the same order as the input array.

Collection

Purpose

used

Reserved original names and generated names

assigned

Names already assigned to processed entries

result

Final output in input order

5. Why do we reserve all usernames initially?

Consider this example:

Code
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:

C#
HashSet<string> used = new HashSet<string>(arr);

Initially:

Code
used = { "a", "a1" }

This prevents us from accidentally generating "a1" before its original entry is processed.

6. Step-by-step dry run

Input

Code
arr = ["a", "a", "a1", "a"]

Step 1: Initialize collections

Code
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.

C#
if (!assigned.Contains(name))

The condition is true.

We add "a" to assigned and result.

Code
assigned = { "a" }
result = [ "a" ]

Step 3: Process the second "a"

The username "a" is already assigned.

We start searching for a suffix:

Code
a1

But "a1" is reserved in used.

So, we continue:

Code
a2

"a2" is available.

We add it to assigned, used, and result.

C#
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.

C#
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

a1

Reserved / assigned

a2

Already generated

a3

Available

We assign "a3".

Final result:

Code
["a", "a2", "a1", "a3"]

7. Understanding the suffix-generation loop

This is the most important part of the solution.

C#
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:

Code
aba1
aba2
aba3
aba4
...

For each candidate, we check:

C#
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:

Code
aba1

If it is occupied, the loop continues with:

Code
aba2

If that is also occupied, it tries:

Code
aba3

And so on.

8. Line-by-line code explanation

Step 1: Create the reserved username set

C#
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

C#
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

C#
List<string> result = new List<string>();

Stores the generated usernames in the original input order.

Step 4: Iterate through the input

C#
foreach (string name in arr)

We process each name sequentially.

Step 5: Keep the original name if available

C#
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

C#
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

C#
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

C#
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

C#
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.