Joseph's team has been assigned the task of creating user-ids for all participants of an online gaming competition. Joseph has designed a process for generating the user-id using the participant's First_Name, Last_Name, PIN code, and a number N. The process defined is as below:
Step 1: Compare the lengths of First_Name and Last_Name of the participant.
The one that is shorter will be called "Smaller Name" and the one that is longer will be called the "Longer Name".
If both are of equal length, then the name that appears earlier in alphabetical order will be called "Smaller Name" and the name that appears later will be called the "Longer Name".
Step 2: The user-id should be generated as below:
Last Letter of the longer name + Entire word of the smaller name + Digit at position N in the PIN when traversing from left to right + Digit at position N in the PIN when traversing from right to left.
Step 3: Toggle the alphabets of the user-id generated in step 2 (i.e., upper-case alphabets should become lower-case and lower-case alphabets should become upper-case).
Example:
First_Name = Rajiv
Last_Name = Roy
PIN = 560037
N = 6
Explanation:
Shorter name is "Roy" (length 3), Longer name is "Rajiv" (length 5).
Last letter of longer name = 'v'. Entire smaller name = "Roy". 6th digit from left in PIN = '7'. 6th digit from right in PIN = '5'. Generated string: vRoy75
We first determine the shorter and longer names by comparing their lengths, using alphabetical order as a strict tie-breaker.
Next, we extract the required characters and PIN digits using simple 0-based index mathematics to build our raw user ID string.
Finally, we loop through this newly constructed string and swap the uppercase and lowercase letters using standard character functions.
Approach
1. Sorting the Names
This is a fantastic string manipulation challenge that tests your attention to detail! Picture a scenario where you are building a secure registration system; the rules might seem arbitrary, but breaking them down step-by-step makes the code write itself. We first check the lengths of the first and last names. If they are exactly the same length, we can just use the standard < operator in C++, which automatically compares strings in alphabetical (lexicographical) order for us!
2. Building and Toggling
When extracting the Nth digit from the PIN, remember that strings in C++ are 0-indexed, but the problem gives N as a 1-based position. So, the N-th digit from the left is at index N - 1, and the Nth digit from the right is at length - N. Once we glue the pieces together, we just loop through the final string and use the built-in islower(), toupper(), isupper(), and tolower() functions to flip the capitalization.
C++ Code-:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string generateUserId(string first, string last, string pin, int n) {
string smaller, longer;
// Step 1: Find smaller and longer names
if (first.length() < last.length()) {
smaller = first;
longer = last;
} else if (first.length() > last.length()) {
smaller = last;
longer = first;
} else {
// Tie-breaker: alphabetical order
if (first < last) {
smaller = first;
longer = last;
} else {
smaller = last;
longer = first;
}
}
// Step 2: Build the raw user-id
char lastChar = longer[longer.length() - 1];
char leftDigit = pin[n - 1];
char rightDigit = pin[pin.length() - n];
string rawId = "";
rawId += lastChar;
rawId += smaller;
rawId += leftDigit;
rawId += rightDigit;
// Step 3: Toggle the cases
for (int i = 0; i < rawId.length(); i++) {
if (islower(rawId[i])) {
rawId[i] = toupper(rawId[i]);
} else if (isupper(rawId[i])) {
rawId[i] = tolower(rawId[i]);
}
}
return rawId;
}
Time Complexity
Time: O(L) - Where L is the combined length of the names, as string comparison and case toggling scale linearly with the length of the words.
Space: O(L) - To store and build the newly generated user ID string.
Problem 1 User Id Generation
Topics Involved / Prerequisites
String Manipulation
ASCII / Case Toggling
Overview
Approach
1. Sorting the Names
This is a fantastic string manipulation challenge that tests your attention to detail! Picture a scenario where you are building a secure registration system; the rules might seem arbitrary, but breaking them down step-by-step makes the code write itself. We first check the lengths of the first and last names. If they are exactly the same length, we can just use the standard
<operator in C++, which automatically compares strings in alphabetical (lexicographical) order for us!2. Building and Toggling
When extracting the Nth digit from the PIN, remember that strings in C++ are 0-indexed, but the problem gives
Nas a 1-based position. So, the N-th digit from the left is at indexN - 1, and the Nth digit from the right is atlength - N. Once we glue the pieces together, we just loop through the final string and use the built-inislower(),toupper(),isupper(), andtolower()functions to flip the capitalization.C++ Code-:
Time Complexity
Time: O(L) - Where L is the combined length of the names, as string comparison and case toggling scale linearly with the length of the words.
Space: O(L) - To store and build the newly generated user ID string.