Problem overview
The function creatingIds builds a user ID from a first name and a last name. The ID is the first 2 letters of first, followed by the last 3 letters of last.
For "John" and "Smith" the ID is "Joith": "Jo" comes from the start of John, and "ith" from the end of Smith. When a name has exactly the number of letters needed, it is used whole, so "Jo" and "Doe" give "JoDoe". Letters keep their case: "ada" and "Lovelace" give "adace", with no capital added.
Approach
Each half of the ID is a piece of one string, and the two pieces are joined. The first piece is at the beginning of first, so it starts at index 0 and has a known length, 2. The second piece is at the end of last. Its length is known too, 3, but its starting index depends on how long the name is.
The last 3 letters of a string of length L start at index L - 3. For "Smith", the length is 5, so the piece starts at index 2 and is "ith". The same rule works for any name with at least three letters, which the constraints guarantee.
Step by step
- Take the first 2 letters of
first.first.substr(0, 2)starts at index 0 and takes 2 characters. The second argument ofsubstris how many characters to keep. - Take the last 3 letters of
last.last.substr(last.size() - 3)starts at the length minus 3. With no second argument,substrruns to the end of the string, so no count is needed. - Join the pieces with
+and return the result.
The solution in C++
class Solution {
public:
string creatingIds(string first, string last) {
return first.substr(0, 2) + last.substr(last.size() - 3);
}
};
C++ details worth knowing here:
- Two forms of
substr.substr(pos, count)takes at mostcountcharacters frompos, andsubstr(pos)takes everything fromposto the end. On"John",substr(0, 2)is"Jo"andsubstr(2)is"hn". - Counting from the end. C++ strings have no negative indexes, so the start of "the last 3 letters" has to be computed from
size(). size()is unsigned. It returns an unsigned type, solast.size() - 3wraps around to a huge number iflasthas fewer than 3 letters. The constraints rule that out here.- Nothing is changed in place.
substrreturns a new string each time, and the original names are left as they were.
Edge cases and common mistakes
- Reading the second argument as an end index. In
first.substr(0, 2)the 2 is a count: two characters starting at index 0, which are the characters at indexes 0 and 1. - Using the wrong string's length. The start of the second piece must use
last.size(), notfirst.size(). The two names usually have different lengths. - Names of exactly the minimum length. With
"Jo"and"Doe", the calllast.substr(0)returns"Doe"andfirst.substr(0, 2)returns"Jo". Both pieces are the whole names, and no special case is needed. - A last name that is too short. With a last name of 2 letters,
last.size() - 3wraps around andsubstrthrowsstd::out_of_range. The lesson's inputs never have such a name. - Changing the case. The ID uses the letters exactly as given. Don't capitalise or lower-case anything.
Testing
"John"and"Smith"give"Joith"."Jo"and"Doe"give"JoDoe"."ada"and"Lovelace"give"adace".
