Program for assigning usernames using Trie

Suppose there is a queue of n users and your task is to assign a username to them. The system works in the following way. Every user has preferred login in the form of a string ā€˜s’ s consists only of small case letters and numbers. User name is assigned in the following order s, s0, s1, s2….s11…. means first you check s if s is available assign it if it is already occupied check for s0 if it is free assign it or if it is occupied check s1 and so on… if a username is assigned to one user it becomes occupied for other users after him in the queue.
Examples:Ā 
Ā 

Input: names[] = {abc, bcd}Ā 
Output: user_names[] = {abc bcd}
Input: names[] = {abc, bcd, abc}Ā 
Output: user_names[] = {abc bcd abc0}
Input : names[] = {geek, geek0, geek1, geek}Ā 
Output : user_names[] = {geek geek0 geek1 geek2}Ā 
For first user geek is free so it is assigned to him similarly for the second and third user but for fourth user geek is not free so we will check geek0 but it is also not free then we will go for geek1 but it is also not free then we will check geek2 it is free so it is assigned to him.

Ā 

We solve this problem using Trie. We do not use usual Trie which have 26 children but a Trie where nodes have 36 children 26 alphabets(a-z) and 10 numbers from (0-9). In addition to this each node of Trie will also have bool variable which will turn into true when a string ending at that node is inserted there will be a int variable as well lets call it add which will be initially -1 and suppose the string is geek and this int variable is equal to -1 then it means that we will directly return geek as it is asked for the first time but suppose it is 12 then it means that string geek, geek0…..geek12 are already present in the Trie.Ā 
StepsĀ 
Step 1: Maintain a Trie as discussed above.Ā 
Step 2: For every given name, check if the string given by user is not in the Trie then return the same string else start from i=add+1 (add is discussed above) and start checking if we concatenate i with the input string is present in the Trie or not if it is not present then return it and set add=i as well as insert it into Trie as well else increment iĀ 
Suppose string is geek and i=5 check if geek5 is in Trie or not if it is not present return geek5 set add for geek = 5 insert geek5 in Trie else if it is not present follow same steps for geek6 until you find a string that is not present in the Trie.Ā 
Ā 

CPP




// C++ program to assign usernames to users
#include <bits/stdc++.h>
using namespace std;
Ā 
#define MAX_CHAR 26
Ā 
struct additional {
Ā 
Ā Ā Ā Ā // is checks if the current node is
Ā Ā Ā Ā // leaf node or not
Ā Ā Ā Ā bool is;
Ā 
Ā Ā Ā Ā // add counts number of concatenations
Ā Ā Ā Ā // of string are present in Trie
Ā Ā Ā Ā int add;
};
Ā 
// represents Trie node
struct Trie {
Ā 
Ā Ā Ā Ā // MAX_CHAR character children
Ā Ā Ā Ā Trie* character[MAX_CHAR];
Ā 
Ā Ā Ā Ā // 10 numbers (from 0 to 9)
Ā Ā Ā Ā Trie* number[10];
Ā 
Ā Ā Ā Ā // one additional struct children
Ā Ā Ā Ā additional a;
};
Ā 
// function to get new node
Trie* getnew()
{
Ā Ā Ā Ā // initialising the Trie node
Ā Ā Ā Ā Trie* node = new Trie;
Ā Ā Ā Ā node->a.is = false;
Ā Ā Ā Ā node->a.add = -1;
Ā Ā Ā Ā for (int i = 0; i < MAX_CHAR; i++)
Ā Ā Ā Ā Ā Ā Ā Ā node->character[i] = NULL;
Ā Ā Ā Ā for (int i = 0; i < 10; i++)
Ā Ā Ā Ā Ā Ā Ā Ā node->number[i] = NULL;
Ā Ā Ā Ā return node;
}
Ā 
// inserting a string into Trie
void insert(Trie*& head, string s)
{
Ā Ā Ā Ā Trie* curr = head;
Ā Ā Ā Ā for (int i = 0; i < s.length(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (s[i] - 'a' < 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (curr->number[s[i] - '0'] == NULL) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr->number[s[i] - '0'] = getnew();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr->number[s[i] - '0'];
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (curr->character[s[i] - 'a'] == NULL)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr->character[s[i] - 'a'] = getnew();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr->character[s[i] - 'a'];
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā curr->a.is = true;
}
Ā 
// returns the structure additional
additional search(Trie* head, string s)
{
Ā Ā Ā Ā additional x;
Ā Ā Ā Ā x.is = false;
Ā Ā Ā Ā x.add = -1;
Ā 
Ā Ā Ā Ā // if head is null directly return additional x
Ā Ā Ā Ā if (head == NULL)
Ā Ā Ā Ā Ā Ā Ā Ā return x;
Ā Ā Ā Ā Trie* curr = head;
Ā 
Ā Ā Ā Ā // checking if string is present or not and
Ā Ā Ā Ā // accordingly returning x
Ā Ā Ā Ā for (int i = 0; i < s.size(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (s[i] - 'a' < 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr->number[s[i] - '0'];
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr->character[s[i] - 'a'];
Ā Ā Ā Ā Ā Ā Ā Ā if (curr == NULL)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return x;
Ā Ā Ā Ā }
Ā Ā Ā Ā x.is = curr->a.is;
Ā Ā Ā Ā x.add = curr->a.add;
Ā Ā Ā Ā return x;
}
Ā 
// special function to update add variable to z
void update(Trie* head, string s, int z)
{
Ā Ā Ā Ā Trie* curr = head;
Ā Ā Ā Ā for (int i = 0; i < s.size(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (s[i] - 'a' < 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr->number[s[i] - '0'];
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr->character[s[i] - 'a'];
Ā Ā Ā Ā }
Ā Ā Ā Ā curr->a.add = z;
}
Ā 
void printUsernames(string username[], int n)
{
Ā Ā Ā Ā // Initializing a Trie root
Ā Ā Ā Ā Trie* head = getnew();
Ā 
Ā Ā Ā Ā // Assigning usernames one by one
Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā string s = username[i];
Ā Ā Ā Ā Ā Ā Ā Ā additional x = search(head, s);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if string is not present directly return it
Ā Ā Ā Ā Ā Ā Ā Ā if (x.is == false) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << s << endl;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insert(head, s);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // to_string(x) converts integer x into string
Ā Ā Ā Ā Ā Ā Ā Ā else if (x.is == true) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // start from x.add+1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int y = x.add + 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā string x = s;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // continuing searching the string
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // until a free username is found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (1 < 2) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if free username is found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (search(head, x + to_string(y)).is == false) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // print it insert it and update add
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << x << y << endl;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insert(head, x + to_string(y));
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā update(head, s, y);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // else increment y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (search(head, x + to_string(y)).is == true)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}
Ā 
// driver function
int main()
{
Ā Ā Ā Ā string name[] = { "geek", "geek0", "geek1", "geek" };
Ā Ā Ā Ā int n = sizeof(name) / sizeof(name[0]);
Ā Ā Ā Ā printUsernames(name, n);
Ā Ā Ā Ā return 0;
}


Java




// Java program to assign usernames to users
import java.util.HashMap;
import java.util.Map;
Ā 
class Additional {
Ā Ā Ā Ā // is checks if the current node is
Ā Ā Ā Ā // leaf node or not
Ā Ā Ā Ā boolean is;
Ā Ā Ā Ā // add counts number of concatenations
Ā Ā Ā Ā // of string are present in Trie
Ā Ā Ā Ā int add;
Ā 
Ā Ā Ā Ā public Additional() {
Ā Ā Ā Ā Ā Ā Ā Ā this.is = false;
Ā Ā Ā Ā Ā Ā Ā Ā this.add = -1;
Ā Ā Ā Ā }
}
Ā 
// represents Trie node
class Trie {
Ā Ā Ā Ā // character children
Ā Ā Ā Ā Map<Character, Trie> children;
Ā Ā Ā Ā // one additional struct children
Ā Ā Ā Ā Additional a;
Ā 
Ā Ā Ā Ā public Trie() {
Ā Ā Ā Ā Ā Ā Ā Ā this.children = new HashMap<>();
Ā Ā Ā Ā Ā Ā Ā Ā this.a = new Additional();
Ā Ā Ā Ā }
}
Ā 
public class Main {
Ā 
Ā Ā Ā Ā // function to get new node
Ā Ā Ā Ā public static Trie getNewNode() {
Ā Ā Ā Ā Ā Ā Ā Ā // initialising the Trie node
Ā Ā Ā Ā Ā Ā Ā Ā return new Trie();
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // inserting a string into Trie
Ā Ā Ā Ā public static void insert(Trie root, String s) {
Ā Ā Ā Ā Ā Ā Ā Ā Trie curr = root;
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < s.length(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā char c = s.charAt(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (Character.isDigit(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!curr.children.containsKey(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr.children.put(c, getNewNode());
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.children.get(c);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!curr.children.containsKey(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr.children.put(c, getNewNode());
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.children.get(c);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā curr.a.is = true;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // returns the structure additional
Ā Ā Ā Ā public static Additional search(Trie root, String s) {
Ā Ā Ā Ā Ā Ā Ā Ā Additional x = new Additional();
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if head is null directly return additional x
Ā Ā Ā Ā Ā Ā Ā Ā if (root == null) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return x;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Trie curr = root;
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // checking if string is present or not and
Ā Ā Ā Ā Ā Ā Ā Ā // accordingly returning x
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < s.length(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā char c = s.charAt(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!curr.children.containsKey(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return x;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.children.get(c);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā x.is = curr.a.is;
Ā Ā Ā Ā Ā Ā Ā Ā x.add = curr.a.add;
Ā Ā Ā Ā Ā Ā Ā Ā return x;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // special function to update add variable to z
Ā Ā Ā Ā public static void update(Trie root, String s, int z) {
Ā Ā Ā Ā Ā Ā Ā Ā Trie curr = root;
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < s.length(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā char c = s.charAt(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.children.get(c);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā curr.a.add = z;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public static void printUsernames(String[] username, int n) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Initializing a Trie root
Ā Ā Ā Ā Ā Ā Ā Ā Trie root = getNewNode();
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Assigning usernames one by one
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String s = username[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Additional x = search(root, s);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if string is not present directly return it
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!x.is) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(s);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insert(root, s);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // to_string(x) converts integer x into string
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // start from x.add+1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int y = x.add + 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String xString = s;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // continuing searching the string
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // until a free username is found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (true) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if free username is found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!search(root, xString + y).is) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // print it insert it and update add
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(xString + y);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insert(root, xString + y);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā update(root, xString, y);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // else increment y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // driver function
public static void main(String[] args) {
Ā Ā Ā Ā String[] name = { "geek", "geek0", "geek1", "geek" };
Ā Ā Ā Ā int n = name.length;
Ā Ā Ā Ā printUsernames(name, n);
}
}
Ā 
// This code is contributed by Aman Kumar.


Python




# python program to assign usernames to users
Ā 
MAX_CHAR = 26
Ā 
class Additional:
Ā Ā Ā Ā # is checks if the current node is
Ā Ā Ā Ā # leaf node or not
Ā Ā Ā Ā def __init__(self):
Ā Ā Ā Ā Ā Ā Ā Ā self.is_set = False
Ā Ā Ā Ā Ā Ā Ā Ā # add counts number of concatenations
Ā Ā Ā Ā Ā Ā Ā Ā # of string are present in Trie
Ā Ā Ā Ā Ā Ā Ā Ā self.add = -1
Ā 
# represents Trie node
class Trie:
Ā Ā Ā Ā def __init__(self):
Ā Ā Ā Ā Ā Ā Ā Ā # MAX_CHAR character children
Ā Ā Ā Ā Ā Ā Ā Ā self.character = [None] * MAX_CHAR
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # 10 numbers (from 0 to 9)
Ā Ā Ā Ā Ā Ā Ā Ā self.number = [None] * 10
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # one additional struct children
Ā Ā Ā Ā Ā Ā Ā Ā self.a = Additional()
Ā 
# function to get new node
def get_new():
Ā Ā Ā Ā # initialising the Trie node
Ā Ā Ā Ā node = Trie()
Ā Ā Ā Ā node.a.is_set = False
Ā Ā Ā Ā node.a.add = -1
Ā Ā Ā Ā return node
Ā 
# inserting a string into Trie
def insert(head, s):
Ā Ā Ā Ā curr = head
Ā Ā Ā Ā for i in range(len(s)):
Ā Ā Ā Ā Ā Ā Ā Ā if ord(s[i]) - ord('a') < 0:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if curr.number[int(s[i])] is None:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr.number[int(s[i])] = get_new()
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.number[int(s[i])]
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if curr.character[ord(s[i]) - ord('a')] is None:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr.character[ord(s[i]) - ord('a')] = get_new()
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.character[ord(s[i]) - ord('a')]
Ā Ā Ā Ā curr.a.is_set = True
Ā 
# returns the structure additional
def search(head, s):
Ā Ā Ā Ā x = Additional()
Ā Ā Ā Ā x.is_set = False
Ā Ā Ā Ā x.add = -1
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # if head is null directly return additional x
Ā Ā Ā Ā if head is None:
Ā Ā Ā Ā Ā Ā Ā Ā return x
Ā 
Ā Ā Ā Ā curr = head
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # checking if string is present or not and
Ā Ā Ā Ā # accordingly returning x
Ā Ā Ā Ā for i in range(len(s)):
Ā Ā Ā Ā Ā Ā Ā Ā if ord(s[i]) - ord('a') < 0:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.number[int(s[i])]
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.character[ord(s[i]) - ord('a')]
Ā Ā Ā Ā Ā Ā Ā Ā if curr is None:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return x
Ā 
Ā Ā Ā Ā x.is_set = curr.a.is_set
Ā Ā Ā Ā x.add = curr.a.add
Ā Ā Ā Ā return x
Ā 
# special function to update add variable to z
def update(head, s, z):
Ā Ā Ā Ā curr = head
Ā Ā Ā Ā for i in range(len(s)):
Ā Ā Ā Ā Ā Ā Ā Ā if ord(s[i]) - ord('a') < 0:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.number[int(s[i])]
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.character[ord(s[i]) - ord('a')]
Ā Ā Ā Ā curr.a.add = z
Ā 
def print_usernames(username):
Ā Ā Ā Ā # Initializing a Trie root
Ā Ā Ā Ā head = get_new()
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # Assigning usernames one by one
Ā Ā Ā Ā for s in username:
Ā Ā Ā Ā Ā Ā Ā Ā x = search(head, s)
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # if string is not present directly return it
Ā Ā Ā Ā Ā Ā Ā Ā if not x.is_set:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print(s)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insert(head, s)
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # to_string(x) converts integer x into string
Ā Ā Ā Ā Ā Ā Ā Ā elif x.is_set:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # start from x.add+1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y = x.add + 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā t = s
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # continuing searching the string
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # until a free username is found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while True:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # if free username is found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if not search(head, t + str(y)).is_set:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # print it insert it and update add
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print(t + str(y))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insert(head, t + str(y))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā update(head, s, y)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # else increment y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elif search(head, t + str(y)).is_set:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y += 1
Ā 
# driver function
if __name__ == '__main__':
Ā Ā Ā Ā name = ["geek", "geek0", "geek1", "geek"]
Ā Ā Ā Ā print_usernames(name)
Ā 
Ā 
# this code is contributed by bhardwajji


C#




// C# program to assign usernames to users
using System;
using System.Collections.Generic;
Ā 
public class Additional {
Ā Ā Ā Ā // is checks if the current node is
Ā Ā Ā Ā // leaf node or not
Ā Ā Ā Ā public bool IS;
Ā Ā Ā Ā // add counts number of concatenations
Ā Ā Ā Ā // of string are present in Trie
Ā Ā Ā Ā public int add;
Ā 
Ā Ā Ā Ā public Additional() {
Ā Ā Ā Ā Ā Ā Ā Ā IS = false;
Ā Ā Ā Ā Ā Ā Ā Ā add = -1;
Ā Ā Ā Ā }
}
Ā 
// represents Trie node
public class Trie {
Ā Ā Ā Ā // character children
Ā Ā Ā Ā public Dictionary<char, Trie> children;
Ā Ā Ā Ā // one additional struct children
Ā Ā Ā Ā public Additional a;
Ā 
Ā Ā Ā Ā public Trie() {
Ā Ā Ā Ā Ā Ā Ā Ā children = new Dictionary<char, Trie>();
Ā Ā Ā Ā Ā Ā Ā Ā a = new Additional();
Ā Ā Ā Ā }
}
Ā 
public class MainClass {
Ā Ā Ā Ā // function to get new node
Ā Ā Ā Ā public static Trie GetNewNode() {
Ā Ā Ā Ā Ā Ā Ā Ā // initialising the Trie node
Ā Ā Ā Ā Ā Ā Ā Ā return new Trie();
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // inserting a string into Trie
Ā Ā Ā Ā public static void Insert(Trie root, string s) {
Ā Ā Ā Ā Ā Ā Ā Ā Trie curr = root;
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < s.Length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā char c = s[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (Char.IsDigit(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!curr.children.ContainsKey(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr.children = GetNewNode();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.children;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!curr.children.ContainsKey(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr.children = GetNewNode();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.children;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā curr.a.IS = true;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // returns the structure additional
Ā Ā Ā Ā public static Additional Search(Trie root, string s) {
Ā Ā Ā Ā Ā Ā Ā Ā Additional x = new Additional();
Ā Ā Ā Ā Ā Ā Ā Ā // if head is null directly return additional x
Ā Ā Ā Ā Ā Ā Ā Ā if (root == null) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return x;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Trie curr = root;
Ā Ā Ā Ā Ā Ā Ā Ā // checking if string is present or not and
Ā Ā Ā Ā Ā Ā Ā Ā // accordingly returning x
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < s.Length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā char c = s[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!curr.children.ContainsKey(c)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return x;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.children;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā x.IS = curr.a.IS;
Ā Ā Ā Ā Ā Ā Ā Ā x.add = curr.a.add;
Ā Ā Ā Ā Ā Ā Ā Ā return x;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // special function to update add variable to z
Ā Ā Ā Ā public static void Update(Trie root, string s, int z) {
Ā Ā Ā Ā Ā Ā Ā Ā Trie curr = root;
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < s.Length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā char c = s[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.children;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā curr.a.add = z;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public static void PrintUsernames(string[] username, int n) {
Ā Ā Ā Ā Ā Ā Ā Ā // Initializing a Trie root
Ā Ā Ā Ā Ā Ā Ā Ā Trie root = GetNewNode();
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Assigning usernames one by one
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā string s = username[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Additional x = Search(root, s);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if string is not present directly return it
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!x.IS) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(s);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Insert(root, s);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // to_string(x) converts integer x into string
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // start from x.add+1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int y = x.add + 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā string xString = s;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // continuing searching the string
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // until a free username is found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (true) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if free username is found
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!Search(root, xString + y).IS) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // print it insert it and update add
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(xString + y);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Insert(root, xString + y);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Update(root, xString, y);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // else increment y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā // driver function
Ā Ā Ā Ā public static void Main() {
Ā Ā Ā Ā Ā Ā Ā Ā string[] name = { "geek", "geek0", "geek1", "geek" };
Ā Ā Ā Ā Ā Ā Ā Ā int n = name.Length;
Ā Ā Ā Ā Ā Ā Ā Ā PrintUsernames(name, n);
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by Vaibhav


Javascript




const MAX_CHAR = 26;
Ā 
class Additional {
Ā Ā constructor() {
Ā Ā Ā Ā this.is_set = false;
Ā Ā Ā Ā this.add = -1;
Ā Ā }
}
Ā 
class Trie {
Ā Ā constructor() {
Ā Ā Ā Ā this.character = Array(MAX_CHAR).fill(null);
Ā Ā Ā Ā this.number = Array(10).fill(null);
Ā Ā Ā Ā this.a = new Additional();
Ā Ā }
}
Ā 
function get_new() {
Ā Ā const node = new Trie();
Ā Ā node.a.is_set = false;
Ā Ā node.a.add = -1;
Ā Ā return node;
}
Ā 
function insert(head, s) {
Ā Ā let curr = head;
Ā Ā for (let i = 0; i < s.length; i++) {
Ā Ā Ā Ā if (s.charCodeAt(i) - 'a'.charCodeAt(0) < 0) {
Ā Ā Ā Ā Ā Ā if (curr.number[parseInt(s[i])] === null) {
Ā Ā Ā Ā Ā Ā Ā Ā curr.number[parseInt(s[i])] = get_new();
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā curr = curr.number[parseInt(s[i])];
Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā if (curr.character[s.charCodeAt(i) - 'a'.charCodeAt(0)] === null) {
Ā Ā Ā Ā Ā Ā Ā Ā curr.character[s.charCodeAt(i) - 'a'.charCodeAt(0)] = get_new();
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā curr = curr.character[s.charCodeAt(i) - 'a'.charCodeAt(0)];
Ā Ā Ā Ā }
Ā Ā }
Ā Ā curr.a.is_set = true;
}
Ā 
function search(head, s) {
Ā Ā const x = new Additional();
Ā Ā x.is_set = false;
Ā Ā x.add = -1;
Ā 
Ā Ā if (head === null) {
Ā Ā Ā Ā return x;
Ā Ā }
Ā 
Ā Ā let curr = head;
Ā 
Ā Ā for (let i = 0; i < s.length; i++) {
Ā Ā Ā Ā if (s.charCodeAt(i) - 'a'.charCodeAt(0) < 0) {
Ā Ā Ā Ā Ā Ā curr = curr.number[parseInt(s[i])];
Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā curr = curr.character[s.charCodeAt(i) - 'a'.charCodeAt(0)];
Ā Ā Ā Ā }
Ā Ā Ā Ā if (curr === null) {
Ā Ā Ā Ā Ā Ā return x;
Ā Ā Ā Ā }
Ā Ā }
Ā 
Ā Ā x.is_set = curr.a.is_set;
Ā Ā x.add = curr.a.add;
Ā Ā return x;
}
Ā 
function update(head, s, z) {
Ā Ā let curr = head;
Ā Ā for (let i = 0; i < s.length; i++) {
Ā Ā Ā Ā if (s.charCodeAt(i) - 'a'.charCodeAt(0) < 0) {
Ā Ā Ā Ā Ā Ā curr = curr.number[parseInt(s[i])];
Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā curr = curr.character[s.charCodeAt(i) - 'a'.charCodeAt(0)];
Ā Ā Ā Ā }
Ā Ā }
Ā Ā curr.a.add = z;
}
Ā 
function print_usernames(username) {
Ā Ā const head = get_new();
Ā Ā for (let s of username) {
Ā Ā Ā Ā const x = search(head, s);
Ā Ā Ā Ā if (!x.is_set) {
Ā Ā Ā Ā Ā Ā console.log(s);
Ā Ā Ā Ā Ā Ā insert(head, s);
Ā Ā Ā Ā } else if (x.is_set) {
Ā Ā Ā Ā Ā Ā let y = x.add + 1;
Ā Ā Ā Ā Ā Ā let t = s;
Ā Ā Ā Ā Ā Ā while (true) {
Ā Ā Ā Ā Ā Ā Ā Ā if (!search(head, t + y.toString()).is_set) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā console.log(t + y.toString());
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā insert(head, t + y.toString());
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā update(head, s, y);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā } else if (search(head, t + y.toString()).is_set) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y += 1;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā }
}
Ā 
const name = ["geek", "geek0", "geek1", "geek"];
print_usernames(name);


Output

geek
geek0
geek1
geek2



Approach: Using a HashSet

Below is the code implementation of the above approach:

C++




// C++ program of the above approach
Ā 
#include <iostream>
#include <unordered_set>
#include <vector>
Ā 
using namespace std;
Ā 
vector<string> assignUsernames(const vector<string>& names)
{
Ā Ā Ā Ā // HashSet to store occupied usernames
Ā Ā Ā Ā unordered_set<string> occupiedUsernames;
Ā Ā Ā Ā // Vector to store assigned usernames
Ā Ā Ā Ā vector<string> userNames;
Ā 
Ā Ā Ā Ā // Iterate through each name in the input vector
Ā Ā Ā Ā for (const string& name : names) {
Ā Ā Ā Ā Ā Ā Ā Ā string assignedName = name;
Ā Ā Ā Ā Ā Ā Ā Ā int count = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā while (occupiedUsernames.count(assignedName) > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Append suffix to assignedName
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā assignedName = name + to_string(count);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā userNames.push_back(assignedName);
Ā Ā Ā Ā Ā Ā Ā Ā occupiedUsernames.insert(assignedName);
Ā Ā Ā Ā }
Ā Ā Ā Ā // Return the vector of assigned usernames
Ā Ā Ā Ā return userNames;
}
Ā 
// Driver Code
int main()
{
Ā Ā Ā Ā vector<string> names = { "abc", "bcd", "abc" };
Ā Ā Ā Ā vector<string> assignedUsernames
Ā Ā Ā Ā Ā Ā Ā Ā = assignUsernames(names);
Ā 
Ā Ā Ā Ā for (const string& username : assignedUsernames) {
Ā Ā Ā Ā Ā Ā Ā Ā cout << username << " ";
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return 0;
}


Java




import java.util.HashSet;
import java.util.Vector;
Ā 
public class Main {
Ā 
Ā Ā Ā Ā // Function to assign usernames to a list of names
Ā Ā Ā Ā public static Vector<String> assignUsernames(Vector<String> names) {
Ā Ā Ā Ā Ā Ā Ā Ā // HashSet to store occupied usernames
Ā Ā Ā Ā Ā Ā Ā Ā HashSet<String> occupiedUsernames = new HashSet<>();
Ā Ā Ā Ā Ā Ā Ā Ā // Vector to store assigned usernames
Ā Ā Ā Ā Ā Ā Ā Ā Vector<String> userNames = new Vector<>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Iterate through each name in the input vector
Ā Ā Ā Ā Ā Ā Ā Ā for (String name : names) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā String assignedName = name;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int count = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (occupiedUsernames.contains(assignedName)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Append suffix to assignedName
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā assignedName = name + count;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā userNames.add(assignedName);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā occupiedUsernames.add(assignedName);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā // Return the vector of assigned usernames
Ā Ā Ā Ā Ā Ā Ā Ā return userNames;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public static void main(String[] args) {
Ā Ā Ā Ā Ā Ā Ā Ā Vector<String> names = new Vector<>();
Ā Ā Ā Ā Ā Ā Ā Ā names.add("abc");
Ā Ā Ā Ā Ā Ā Ā Ā names.add("bcd");
Ā Ā Ā Ā Ā Ā Ā Ā names.add("abc");
Ā Ā Ā Ā Ā Ā Ā Ā Vector<String> assignedUsernames = assignUsernames(names);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā for (String username : assignedUsernames) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.print(username + " ");
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}


C#




// C# program of the above approach
using System;
using System.Collections.Generic;
Ā 
class Program
{
Ā Ā Ā Ā static List<string> AssignUsernames(List<string> names)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // HashSet to store occupied usernames
Ā Ā Ā Ā Ā Ā Ā Ā HashSet<string> occupiedUsernames = new HashSet<string>();
Ā Ā Ā Ā Ā Ā Ā Ā // List to store assigned usernames
Ā Ā Ā Ā Ā Ā Ā Ā List<string> userNames = new List<string>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Iterate through each name in the input list
Ā Ā Ā Ā Ā Ā Ā Ā foreach (string name in names)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā string assignedName = name;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int count = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (occupiedUsernames.Contains(assignedName))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Append suffix to assignedName
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā assignedName = name + count.ToString();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā userNames.Add(assignedName);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā occupiedUsernames.Add(assignedName);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā // Return the list of assigned usernames
Ā Ā Ā Ā Ā Ā Ā Ā return userNames;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Driver Code
Ā Ā Ā Ā static void Main()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā List<string> names = new List<string> { "abc", "bcd", "abc" };
Ā Ā Ā Ā Ā Ā Ā Ā List<string> assignedUsernames = AssignUsernames(names);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā foreach (string username in assignedUsernames)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(username + " ");
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}


Output

abc bcd abc0 


Time Complexity: O(nm), where n is the number of names in the input vector and m is the length of the longest name.
Auxiliary Space: O(nm)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button