Related Articles

# Program for assigning usernames using Trie

• Difficulty Level : Hard
• Last Updated : 28 Sep, 2018

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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

 `// C++ program to assign usernames to users``#include ``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;`` ` `    ``// 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)``{``    ``// Initialing 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 inser 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);``    ``printUsernames(name, n);``    ``return` `0;``}`
Output:
```geek
geek0
geek1
geek2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up