Problem overview
A user ID is built from a name: the first 2 letters of the first name, then the last 3 letters of the last name. The function creatingIds receives first and last and returns that ID. Letters keep their case.
For "John" and "Smith" the answer is "Joith": "Jo" from the first name and "ith" from the last. For "Jo" and "Doe" the names are exactly 2 and 3 letters long, so both are used whole and the result is "JoDoe". For "ada" and "Lovelace" the result is "adace".
Approach
Both halves can be cut with substring. The first 2 letters of first are the characters at indexes 0 and 1. The last 3 letters of last start at index last.length - 3 and run to the end. Then + joins the two pieces.
Step by step
- Take the first 2 letters of
firstwithfirst.substring(0, 2). The end index ofsubstring(start, end)is exclusive, so0, 2keeps indexes 0 and 1. - Take the last 3 letters of
lastwithlast.substring(last.length - 3). With only a start index,substringgoes to the end of the string. - Join the two pieces with
+and return the result.
The solution in Kotlin
class Solution {
fun creatingIds(first: String, last: String): String {
return first.substring(0, 2) + last.substring(last.length - 3)
}
}
Details worth knowing:
- Start inclusive, end exclusive. This is how
substring(0, 2)gives exactly two characters: the length of the piece is end minus start. - Counting from the end. There are no negative indexes in Kotlin. To count from the end, subtract from the length: for
"Smith"the length is 5, solast.length - 3is 2, andsubstring(2)is"ith". - Shortcuts.
first.take(2)andlast.takeLast(3)give the same pieces. They also don't throw when a string is too short, which can hide a mistake. - Case is untouched.
substringcopies characters as they are, so the ID needs no extra step to keep upper and lower case.
Tracing the examples
For "John" and "Smith": "John".substring(0, 2) is "Jo". The length of "Smith" is 5, so last.length - 3 is 2, and "Smith".substring(2) is "ith". Joined: "Joith".
For "ada" and "Lovelace": the first piece is "ad". The length of "Lovelace" is 8, so the start index is 5 and the piece is "ace". The ID is "adace", and the lower-case a of the first name stays lower case.
The start index depends on the length of last, so the same expression works for names of any length, from 3 letters up.
Two slices, one pattern
The exercise combines the two most common slices of a string: a prefix, which starts at index 0 and needs an end index, and a suffix, which ends at the end of the string and needs a start index. The prefix length is a fixed number, 2. The suffix start depends on the string, so it is computed from the length.
When you write a slice, check it with the shortest input the lesson allows. For the first name that is "Jo", where substring(0, 2) is the whole name. For the last name it is "Doe", where the start index 3 - 3 is 0. Both land exactly on the boundary and still work, which is a good sign that the indexes are right.
Edge cases and common mistakes
- Exact lengths. A first name of exactly 2 letters and a last name of exactly 3 work:
"Doe".substring(0)is the whole name. - End index 3 instead of 2.
first.substring(0, 3)returns three letters, one too many. - Using the last index as the start.
last.substring(last.length - 1)returns only the final letter. Subtract 3 for three letters. - Names that are too short. The lesson promises at least 2 and 3 letters. Otherwise
substringwould throw aStringIndexOutOfBoundsException, for example on"a".substring(0, 2).
Testing
"John","Smith"gives"Joith"."Jo","Doe"gives"JoDoe"."ada","Lovelace"gives"adace".
