You are tasked with generating unique usernames for a list of employees. Each username must be derived from the employee's full name, follow a specific format, and be distinct from all others.
Given an array of strings names, where each string represents a full name composed of a given name, optional middle names, and a family name (separated by spaces), you need to produce a list of usernames in the same order as the input.
The username generation must adhere to these rules:
- Uniqueness: No two usernames may be identical.
- Allowed characters: Only lowercase English letters and digits are permitted.
- Maximum length: A username cannot exceed 8 characters.
- Construction: The username is built from the family name and the given name (middle names are ignored). Initial attempts prioritize characters from the family name, followed by the given name to reach the length limit.
Before processing, strip any hyphens (-) and apostrophes (') from the name.
For each name, try to generate a username using the following steps in order:
- Standard approach: Form a username by taking up to 7 characters from the family name and then appending characters from the given name until the total length is 8 (or both names are exhausted). This yields a username of up to 8 characters, favoring the family name.
- Adjusted approach: If the standard username is already taken, create a variation by reducing the number of characters taken from the family name and increasing the count from the given name. Continue this process as long as the resulting username is not shorter than the standard username.
- Digit suffix: If all adjusted variations are taken or an adjusted username would be shorter than the standard, fall back to the standard username and append a single digit (
1–9). If the standard username is already 8 characters long, first drop the last character from the family name portion to make room for the digit.
Implement the function create_usernames(names) that returns the list of generated usernames.
Constraints:
1 ≤ names.length ≤ 1000
- Names contain only English letters, spaces, hyphens, and apostrophes.
- It is guaranteed that a single digit is enough to resolve any conflicts.
Example 1:
Input: names = ["Peter Middle Parker", "Peter Parker", "Peter A. Parker", "Peter B. Parker", "Peter C. Parker", "Peter D. Parker"]
Output: ["parkerpe", "parkepet", "parkpete", "parpeter", "parkepe1", "parkepe2"]
Explanation:
- "Peter Middle Parker" → "parkerpe" (Default: 6 chars from 'Parker', 2 from 'Peter'). The middle name is ignored.
- "Peter Parker" → "parkepet" (Adjusted: 5 chars from family, 3 from given).
- "Peter A. Parker" → "parkpete" (Adjusted: 4 chars from family, 4 from given).
- "Peter B. Parker" → "parpeter" (Adjusted: 3 chars from family, 5 from given).
- "Peter C. Parker" → Next adjusted is "papeter" (7 chars). The username is rejected because it is shorter than the original "parkerpe" (8 chars). So fallback to Digit Suffix: Base "parkerpe" (8 chars) → drop last char → "parkepe" + "1" → "parkepe1".
- "Peter D. Parker" → "parkepe2".
Example 2:
Input: names = ["John Doe", "John Doe", "John Doe"]
Output: ["doejohn", "doejohn1", "doejohn2"]
Example 3:
Input: names = ["Peter Parker", "Peter Parker", "Peter Parker", "Peter Parker", "Peter Parker", "Peter Parker", "Peter Parker", "Peter Parker"]
Output: ["parkerpe", "parkepet", "parkpete", "parpeter", "parkepe1", "parkepe2", "parkepe3", "parkepe4"]