Start learning free

LESSON 69 OF 285 · STRINGS - CHALLENGES

Creating IDs in Java: substring from Both Ends

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

    JAVA · STRINGS - CHALLENGES

    Creating Ids in Java, explained

    Problem overview

    The method creatingIds builds a user ID from a person's name. It takes the first 2 letters of first and the last 3 letters of last, and joins them in that order. The letters keep their case, and both names have enough letters: first has at least 2 and last at least 3.

    With first = "John" and last = "Smith", the first two letters are Jo and the last three are ith, so the ID is "Joith". With "Jo" and "Doe" both names are used whole, giving "JoDoe". With "ada" and "Lovelace" the result is "adace": nothing is capitalised.

    Approach: one slice from each name

    The two halves are independent. The first half is a slice from the start of first, the second half is a slice from the end of last. Java's substring can produce both, in two different forms.

    For the front of a string, use substring(start, end). The start index is included and the end index is excluded, so substring(0, 2) covers indexes 0 and 1: exactly two letters.

    For the back of a string, you know the length of the piece you want but not its start index, so you compute it. A string of length L has indexes 0 to L - 1. The last three letters begin at L - 3. For "Smith", L is 5, the start is 2, and substring(2) is "ith". The one-argument form runs to the end of the string, so no end index is needed.

    Step by step

    1. Take the first two letters. first.substring(0, 2) returns the first two characters as a new string. The original string is not changed.
    2. Take the last three letters. last.substring(last.length() - 3) starts three characters before the end. Remember the parentheses on length(): for strings it is a method.
    3. Join the pieces and return. The + operator between two strings concatenates them, and the expression can be returned directly.

    The solution in Java

    class Solution {
        public String creatingIds(String first, String last) {
            return first.substring(0, 2) + last.substring(last.length() - 3);
        }
    }
    

    On the three examples, the method returns:

    Joith
    JoDoe
    adace
    

    The solution never changes case, so lower-case ada stays lower case, as the statement requires. If you wanted a capitalised ID, you would have to convert explicitly, for example with toUpperCase() on a one-letter slice, but this lesson doesn't ask for it.

    Edge cases and common mistakes

    • Names of exactly the minimum length. When last has exactly 3 letters, last.length() - 3 is 0 and substring(0) returns the whole name. When first has exactly 2 letters, substring(0, 2) returns it whole. That is the "JoDoe" example.
    • Off-by-one on the end index. first.substring(0, 1) gives only one letter, because the end index is excluded. To keep two letters the end index must be 2.
    • Counting from the wrong place. last.substring(3) drops the first three letters instead of keeping the last three. For "Smith" it gives "th".
    • Names that are too short. The constraints rule these out. With a last name of two letters, last.length() - 3 is -1 and substring(-1) throws a StringIndexOutOfBoundsException. With "Li" the message is Range [-1, 2) out of bounds for length 2.
    • Using charAt. Joining char values with + adds their character codes as numbers instead of building text, so slices are the safer choice for strings of more than one letter.

    Testing

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