Problem overview
The method creatingIds builds a user ID from a person's name. It takes the first 2 letters of first and the last 3 letters of last, and joins them in that order. The letters keep their case, and both names have enough letters: first has at least 2 and last at least 3.
With first = "John" and last = "Smith", the first two letters are Jo and the last three are ith, so the ID is "Joith". With "Jo" and "Doe" both names are used whole, giving "JoDoe". With "ada" and "Lovelace" the result is "adace": nothing is capitalised.
Approach: one slice from each name
The two halves are independent. The first half is a slice from the start of first, the second half is a slice from the end of last. Java's substring can produce both, in two different forms.
For the front of a string, use substring(start, end). The start index is included and the end index is excluded, so substring(0, 2) covers indexes 0 and 1: exactly two letters.
For the back of a string, you know the length of the piece you want but not its start index, so you compute it. A string of length L has indexes 0 to L - 1. The last three letters begin at L - 3. For "Smith", L is 5, the start is 2, and substring(2) is "ith". The one-argument form runs to the end of the string, so no end index is needed.
Step by step
- Take the first two letters.
first.substring(0, 2)returns the first two characters as a new string. The original string is not changed. - Take the last three letters.
last.substring(last.length() - 3)starts three characters before the end. Remember the parentheses onlength(): for strings it is a method. - Join the pieces and return. The
+operator between two strings concatenates them, and the expression can be returned directly.
The solution in Java
class Solution {
public String creatingIds(String first, String last) {
return first.substring(0, 2) + last.substring(last.length() - 3);
}
}
On the three examples, the method returns:
Joith
JoDoe
adace
The solution never changes case, so lower-case ada stays lower case, as the statement requires. If you wanted a capitalised ID, you would have to convert explicitly, for example with toUpperCase() on a one-letter slice, but this lesson doesn't ask for it.
Edge cases and common mistakes
- Names of exactly the minimum length. When
lasthas exactly 3 letters,last.length() - 3is 0 andsubstring(0)returns the whole name. Whenfirsthas exactly 2 letters,substring(0, 2)returns it whole. That is the"JoDoe"example. - Off-by-one on the end index.
first.substring(0, 1)gives only one letter, because the end index is excluded. To keep two letters the end index must be 2. - Counting from the wrong place.
last.substring(3)drops the first three letters instead of keeping the last three. For"Smith"it gives"th". - Names that are too short. The constraints rule these out. With a last name of two letters,
last.length() - 3is -1 andsubstring(-1)throws aStringIndexOutOfBoundsException. With"Li"the message isRange [-1, 2) out of bounds for length 2. - Using
charAt. Joiningcharvalues with+adds their character codes as numbers instead of building text, so slices are the safer choice for strings of more than one letter.
Testing
first = "John",last = "Smith"gives"Joith".first = "Jo",last = "Doe"gives"JoDoe".first = "ada",last = "Lovelace"gives"adace".
