Start learning free

LESSON 70 OF 291 · STRINGS - CHALLENGES

Creating IDs in Kotlin: Slicing Strings with substring

Practice Read the full explanation

A website creates a user ID from each person's name: the first 2 letters of the first name, followed by the last 3 letters of the last name.

Given the first name first and the last name last, return the ID.

 

Example 1:

Input: first = "John", last = "Smith"
Output: "Joith"
Explanation: "Jo" are the first 2 letters of "John" and "ith" are the last 3 letters of "Smith".

Example 2:

Input: first = "Jo", last = "Doe"
Output: "JoDoe"
Explanation: "Jo" has exactly 2 letters and "Doe" exactly 3, so both names are used whole.

Example 3:

Input: first = "ada", last = "Lovelace"
Output: "adace"
Explanation: The letters keep their case: the ID doesn't change any letter to upper or lower case.

 

Constraints:

  • first has at least 2 letters and last has at least 3 letters.
  • Both names are made of English letters only, lowercase or uppercase.
STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · STRINGS - CHALLENGES

    Creating Ids in Kotlin, explained

    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

    1. Take the first 2 letters of first with first.substring(0, 2). The end index of substring(start, end) is exclusive, so 0, 2 keeps indexes 0 and 1.
    2. Take the last 3 letters of last with last.substring(last.length - 3). With only a start index, substring goes to the end of the string.
    3. 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, so last.length - 3 is 2, and substring(2) is "ith".
    • Shortcuts. first.take(2) and last.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. substring copies 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 substring would throw a StringIndexOutOfBoundsException, for example on "a".substring(0, 2).

    Testing

    • "John", "Smith" gives "Joith".
    • "Jo", "Doe" gives "JoDoe".
    • "ada", "Lovelace" gives "adace".
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free