Problem overview
The method creatingIds builds a user ID from two names: the first 2 letters of first, followed by the last 3 letters of last. For first = "John" and last = "Smith" the ID is "Joith": "Jo" from John and "ith" from Smith. When the names are exactly as long as the pieces asked for, such as "Jo" and "Doe", both are used whole and the result is "JoDoe". Case is never changed: "ada" and "Lovelace" give "adace".
Approach
This is two slices glued together. The first slice starts at the beginning and has a fixed length of 2. The second one has a fixed length of 3 but sits at the end of the string, so its start depends on the length of last: it begins at last.length - 3.
The lesson's constraints say first has at least 2 letters and last has at least 3, so both slices always exist.
Counting from the end
Scala strings are indexed from the left, with no negative indexes, so "the last 3 letters" has to be turned into a start position. If a string has length L, its last 3 letters are at the indexes L - 3, L - 2 and L - 1. The slice therefore starts at L - 3 and, since no end is given, runs to the end. The same pattern gives the last k letters of any string: start at length - k.
Step by step
- Take the first two letters of
firstwithfirst.substring(0, 2). The end index is excluded, so 0 up to 2 gives two letters. - Take the last three letters of
lastwithlast.substring(last.length - 3). With one argument,substringruns from that index to the end of the string. - Join the two slices with
+and return the ID.
The solution in Scala
class Solution {
def creatingIds(first: String, last: String): String = {
return first.substring(0, 2) + last.substring(last.length - 3)
}
}
A few Scala details:
substring(start, end)includesstartand excludesend. The length of the slice isend - start.substring(start)goes to the end, so counting back from the end only needs the length:length - 3is the index of the third letter from the right.- Scala strings also have
take(2)andtakeRight(3), which do the same job and don't throw on short strings. This lesson practises index arithmetic withsubstring.
Tracing an example
For first = "John" and last = "Smith": first.substring(0, 2) is "Jo". The length of "Smith" is 5, so the second slice starts at 5 - 3 = 2. "Smith".substring(2) is "ith", because the characters at indexes 2, 3 and 4 are i, t and h. Joined, they make "Joith".
Starting at last.length - 2 instead would give "th", which shows how the subtracted number sets the size of the slice.
Edge cases and common mistakes
- Off-by-one at the end.
last.substring(last.length - 2)returns two letters, not three. The number you subtract is the number of letters you keep. - Wrong end index for the first slice.
first.substring(0, 1)keeps only one letter, since the end index is excluded. - Exactly-sized names. For
"Jo"and"Doe", the second slice starts at index 0, so the whole last name is used. - Names that are too short. Asking for more letters than a string has, as in
"A".substring(0, 2), throws aStringIndexOutOfBoundsException. The lesson's constraints rule this out.
Testing
"John"and"Smith"give"Joith"."Jo"and"Doe"give"JoDoe"."ada"and"Lovelace"give"adace".
