Start learning free

LESSON 70 OF 292 · STRINGS - CHALLENGES

Creating IDs in Swift: prefix and suffix on Strings

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

    SWIFT · STRINGS - CHALLENGES

    Creating Ids in Swift, explained

    Problem overview

    A website builds a user ID from a person's name: the first 2 letters of the first name followed by the last 3 letters of the last name. The function creatingIds receives first and last and returns that ID.

    For "John" and "Smith" the first two letters are Jo and the last three letters of the surname are ith, so the ID is "Joith". For "Jo" and "Doe" the names have exactly the needed lengths, so both are used whole and the ID is "JoDoe". Letters keep their case: "ada" and "Lovelace" give "adace".

    Approach: take pieces from the front and from the back

    The problem needs two pieces of text, one counted from the start of a string and one counted from its end. Swift strings can't be indexed with integers, so there is no last[last.count - 3]. Instead, the standard library offers prefix(n), which keeps the first n characters, and suffix(n), which keeps the last n. You never need to compute a length or a start position.

    The ID is then first.prefix(2) joined with last.suffix(3).

    Step by step

    1. Take the first two letters of first with first.prefix(2).
    2. Take the last three letters of last with last.suffix(3).
    3. Convert each piece to a String. Both methods return a Substring, a lightweight view into the original text, and the function returns a String.
    4. Join the two strings with + and return the result.

    The solution in Swift

    class Solution {
        func creatingIds(_ first: String, _ last: String) -> String {
            return String(first.prefix(2)) + String(last.suffix(3))
        }
    }
    

    Details worth knowing:

    • The underscores. The _ before each parameter name means callers pass the arguments without labels: creatingIds("John", "Smith") instead of creatingIds(first: "John", last: "Smith").
    • Substring, then String. Joining two Substring values with + gives another Substring, which doesn't match the declared return type. Converting each piece with String(...) fixes that.
    • Counting characters. prefix and suffix count Swift characters, not bytes. A letter with an accent written as two Unicode pieces still counts once.
    • Too-short names don't crash. If a name has fewer letters than requested, prefix and suffix return what is there. The lesson guarantees at least 2 and 3 letters, so this only matters if you reuse the idea elsewhere.

    String interpolation gives an equivalent version: "\(first.prefix(2))\(last.suffix(3))". Interpolation turns each Substring into text for you, so no String(...) is needed. For "John" and "Smith" it also gives "Joith".

    The ID has a fixed shape: it is always 5 characters long when the names meet the lesson's limits, 2 from the first name and 3 from the last. Any longer first name or last name only changes which letters are chosen, never the length.

    Edge cases and common mistakes

    • Exactly the minimum length. "Jo" and "Doe" give "JoDoe".
    • Longer names. "AL" and "LOVE" give "ALOVE": only OVE, the last three letters, are used.
    • Swapping the two calls. last.prefix(2) with first.suffix(3) builds an ID from the wrong ends of the wrong names.
    • Changing the case. The ID keeps letters as written. "ada" with "Lovelace" gives "adace", not "Adace".
    • Forgetting String(...). Without it the compiler reports that it can't convert the returned Substring to a String.

    Testing

    • "John", "Smith" gives "Joith".
    • "Jo", "Doe" gives "JoDoe".
    • "ada", "Lovelace" gives "adace".
    • "AL", "LOVE" gives "ALOVE".
    • "Bo", "Li" is outside the lesson's constraints (the last name has only 2 letters), but it returns "BoLi": the code uses what is available.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free