Start learning free

LESSON 70 OF 291 · STRINGS - CHALLENGES

Creating IDs in Scala: Slicing Names 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

    SCALA · STRINGS - CHALLENGES

    Creating Ids in Scala, explained

    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

    1. Take the first two letters of first with first.substring(0, 2). The end index is excluded, so 0 up to 2 gives two letters.
    2. Take the last three letters of last with last.substring(last.length - 3). With one argument, substring runs from that index to the end of the string.
    3. 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) includes start and excludes end. The length of the slice is end - start.
    • substring(start) goes to the end, so counting back from the end only needs the length: length - 3 is the index of the third letter from the right.
    • Scala strings also have take(2) and takeRight(3), which do the same job and don't throw on short strings. This lesson practises index arithmetic with substring.

    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 a StringIndexOutOfBoundsException. The lesson's constraints rule this out.

    Testing

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