Start learning free

LESSON 70 OF 285 · STRINGS - CHALLENGES

Stuttering Function in Java: Building a String with +

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

    JAVA · STRINGS - CHALLENGES

    Stuttering Function in Java, explained

    Problem overview

    The method stutterize takes a word and returns it said with a stutter: its first two characters, then a hyphen, then the whole word. The word has at least two letters and contains English letters only.

    For "dog" the result is "do-dog". For "Hello" it is "He-Hello", where the stutter keeps the upper-case H. For "ab" the first two characters are the whole word, so the result is "ab-ab".

    Approach: three pieces, one expression

    The answer is a fixed shape made of three parts, so the work is picking out the parts and joining them:

    1. the first two characters of word;
    2. the literal hyphen "-";
    3. the complete word.

    Java builds strings from pieces with the + operator. As long as at least one operand is a String, + concatenates, and a chain such as a + "-" + b is evaluated left to right, each step producing a new string.

    To get the first two characters, use substring(0, 2). The start index 0 is included and the end index 2 is excluded, so the slice holds the characters at indexes 0 and 1. A slice stops right before its end index.

    Step by step

    1. Slice the first two characters. word.substring(0, 2) returns a new two-character string. For "dog" it is "do". The lesson guarantees at least two letters, so both indexes always exist.
    2. Add the hyphen. The hyphen is a string literal in double quotes, so the join continues as text.
    3. Add the whole word. The last operand is word itself, unchanged.
    4. Return the result. The whole chain is one expression, so the method can return it directly.

    The solution in Java

    class Solution {
        public String stutterize(String word) {
            return word.substring(0, 2) + "-" + word;
        }
    }
    

    Running this on the three examples prints:

    dog -> do-dog
    Hello -> He-Hello
    ab -> ab-ab
    

    Notice that the method name is stutterize and the parameter is named word, as in the editor. The lesson's title says "function", but in Java a function that belongs to a class is called a method.

    A few details are worth remembering:

    • Strings are immutable. substring never changes word. It returns a fresh string, which is why the whole word can still be appended afterwards.
    • Case is preserved. Slicing copies the characters as they are, so the upper-case H of "Hello" appears in both parts.
    • The hyphen is plain text. It doesn't need escaping. Inside double quotes, only characters such as " and \ need special treatment.

    Edge cases and common mistakes

    • Off-by-one on the end index. word.substring(0, 1) keeps one character, and word.substring(0, 3) keeps three. For the stutter to have two characters, the end index must be 2.
    • Using charAt twice. Writing word.charAt(0) + word.charAt(1) + "-" + word first adds the two character codes as numbers. For "dog", 'd' is 100 and 'o' is 111, so the expression produces 211-dog. Only once a string is part of the chain does + start joining text.
    • Forgetting the hyphen, or adding spaces around it. The expected text for "dog" is exactly do-dog, with no spaces.
    • Words shorter than two letters. The constraints rule them out. For a one-letter word, substring(0, 2) throws a StringIndexOutOfBoundsException; with "a" the message is Range [0, 2) out of bounds for length 1.
    • Two-letter words. "ab" gives "ab-ab". The slice is the whole word, which is correct, not a bug.

    Testing

    • "dog" gives "do-dog".
    • "Hello" gives "He-Hello".
    • "ab" gives "ab-ab".
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free