Start learning free

LESSON 71 OF 291 · STRINGS - CHALLENGES

Stuttering Function in Scala: substring and String Concatenation

Practice Read the full explanation

Given a word word, return it said with a stutter: its first two characters, then a hyphen -, then the whole word.

 

Example 1:

Input: word = "dog"
Output: "do-dog"
Explanation: The first two characters are "do". After them come a hyphen and the whole word, "dog".

Example 2:

Input: word = "Hello"
Output: "He-Hello"
Explanation: The characters keep their case: the stutter starts with an upper-case H, as the word does.

Example 3:

Input: word = "ab"
Output: "ab-ab"
Explanation: A word with only two letters: its first two characters are the whole word.

 

Constraints:

  • word has at least 2 letters.
  • word is made of English letters only, lowercase or uppercase: no spaces, digits or punctuation.
STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · STRINGS - CHALLENGES

    Stuttering Function in Scala, explained

    Problem overview

    The method stutterize receives a word and returns it said with a stutter: its first two characters, then a hyphen, then the whole word. "dog" becomes "do-dog", and "Hello" becomes "He-Hello", with the capital H kept because characters are copied as they are. A two-letter word such as "ab" becomes "ab-ab", since its first two characters are the whole word.

    Approach

    The result has three pieces in a fixed order: the first two characters of the word, the text "-", and the word itself. The only piece that needs any work is the first one, a slice from the start of the string. The lesson guarantees at least two letters, so the slice always exists.

    Slices and indexes

    The two numbers in substring(0, 2) are positions between characters rather than characters themselves. The first is where the slice begins and the second is where it stops, with that position left out. Picture the word "dog" with the cut marks 0 before the d, 1 after it, 2 after the o and 3 after the g. A slice from mark 0 to mark 2 holds "do". Because the end mark is excluded, the length of a slice is always the end minus the start.

    Step by step

    1. Slice the first two characters with word.substring(0, 2). A slice stops right before its end index, so ending at 2 keeps the characters at indexes 0 and 1.
    2. Add the hyphen as the string "-".
    3. Add the whole word and return the three pieces joined with +.

    The solution in Scala

    class Solution {
        def stutterize(word: String): String = {
            return word.substring(0, 2) + "-" + word
        }
    }
    

    A few Scala details:

    • + joins strings from left to right. Each + builds a new string, because Scala strings are immutable, and that is fine for a result this small.
    • The hyphen is written in double quotes, "-". In Scala, double quotes make a String and single quotes make a Char. A Char in this position would still be joined as text, because the left side is a string, but strings are the natural choice here.
    • The method name is stutterize, the lesson's own name, not the lesson's title.

    Tracing an example

    For word = "dog", the first piece is "dog".substring(0, 2), which is "do". Adding "-" gives "do-", and adding the whole word gives "do-dog".

    Changing the slice shows what each index does. With substring(0, 3) on the same word, the first piece is the whole word and the result is "dog-dog". With "Hello" and the correct slice, the result is "He-Hello": the same two letters appear again at the start of the word, upper-case H included.

    Edge cases and common mistakes

    • Slicing too little or too much. substring(0, 1) keeps one character, giving "d-dog". substring(0, 3) keeps three and gives "dog-dog".
    • Two-letter words. "ab".substring(0, 2) is the whole word, and the result is "ab-ab".
    • Words shorter than two letters. A one-letter word makes substring(0, 2) throw a StringIndexOutOfBoundsException. The lesson's constraints exclude this case.
    • Changing case. The first two characters are copied unchanged, so "Hello" starts with an upper-case H both times.

    Testing

    • "dog" gives "do-dog".
    • "Hello" gives "He-Hello".
    • "ab" gives "ab-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