Start learning free

LESSON 68 OF 291 · STRINGS - CHALLENGES

Concat First and Last Char in Scala

Practice Read the full explanation

Given a string s, return a new string made of two characters: the first character of s, followed by its last character.

 

Example 1:

Input: s = "hello"
Output: "ho"
Explanation: The first character is "h" and the last one is "o".

Example 2:

Input: s = "Hello World!"
Output: "H!"
Explanation: Every character counts, so the last one is the "!", not the "d". The "H" stays upper case.

Example 3:

Input: s = "ab"
Output: "ab"
Explanation: With only two characters, the first is "a" and the last is "b".

 

Constraints:

  • s has at least 2 characters.
  • s contains English letters, digits, spaces and punctuation, and doesn't start or end with a space.
STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · STRINGS - CHALLENGES

    Concat First And Last Char in Scala, explained

    Problem overview

    The method concatFirstAndLastChar receives a string s of at least two characters and returns a new two-character string: the first character of s followed by its last character. For "hello" the answer is "ho". For "Hello World!" it is "H!": every character counts, so the last one is the exclamation mark, and the upper-case H stays as it is. For "ab" the first and last characters are the whole string, so the answer is "ab".

    Approach

    A string's characters are numbered from 0, so the first character is at index 0 and the last one is at index s.length - 1. The task is to cut out those two one-character pieces and glue them together.

    Scala offers two ways to read one character. s.charAt(i) gives a Char, and s.substring(i, j) gives a String holding the characters from i up to, but not including, j. The difference matters here. The + between two Char values adds their character codes as numbers, while + between two strings joins them as text. With s = "hello", the expression s.charAt(0) + s.charAt(s.length - 1) gives the number 215 (104 plus 111), not "ho". Slices keep the pieces as strings, so the join works as expected.

    Step by step

    1. Take the first character as a slice with s.substring(0, 1). The end index is excluded, so this is exactly the character at index 0.
    2. Take the last character as a slice with s.substring(s.length - 1). With one argument, substring starts there and runs to the end of the string, which leaves just the last character.
    3. Join the two strings with + and return the result.

    The solution in Scala

    class Solution {
        def concatFirstAndLastChar(s: String): String = {
            return s.substring(0, 1) + s.substring(s.length - 1)
        }
    }
    

    A few Scala details:

    • return is allowed, and the lesson uses it. In Scala the last expression of a block is its value anyway, so the same method also works without the word return.
    • substring is a Java method, available on every Scala String. Its end index is exclusive, which is why substring(0, 1) keeps one character.
    • Scala has other ways to get a character, such as s.head and s.last. They return a Char, so they need .toString before they are joined with + as text.

    Tracing an example

    Take s = "Hello World!". Its length is 12, so the last index is 11. The first slice, s.substring(0, 1), is "H". The second slice starts at s.length - 1, which is 11, and runs to the end: it is "!". Joined, they make "H!". The space in the middle and every letter between the two ends are never touched, because the method only looks at the two ends.

    For "hello" the length is 5, so the second slice starts at index 4 and gives "o". The first slice gives "h", and the answer is "ho".

    Edge cases and common mistakes

    • Joining Chars with +. As shown above, two Char values add up as numbers. Use slices, or convert each Char to a string first.
    • Using s.length as the index. The last valid index is s.length - 1. Starting a slice at s.length gives an empty string, and starting beyond it throws an exception.
    • Two-character strings. For "ab" the two slices are "a" and "b", so the method returns the same string. Nothing special is needed.
    • Trimming or changing case. The method returns the characters exactly as they are, punctuation and capital letters included.

    Testing

    • "hello" gives "ho".
    • "Hello World!" gives "H!".
    • "ab" gives "ab".
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free