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:
- the first two characters of
word; - the literal hyphen
"-"; - 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
- 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. - Add the hyphen. The hyphen is a string literal in double quotes, so the join continues as text.
- Add the whole word. The last operand is
worditself, unchanged. - 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.
substringnever changesword. 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
Hof"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, andword.substring(0, 3)keeps three. For the stutter to have two characters, the end index must be 2. - Using
charAttwice. Writingword.charAt(0) + word.charAt(1) + "-" + wordfirst adds the two character codes as numbers. For"dog",'d'is 100 and'o'is 111, so the expression produces211-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 exactlydo-dog, with no spaces. - Words shorter than two letters. The constraints rule them out. For a one-letter word,
substring(0, 2)throws aStringIndexOutOfBoundsException; with"a"the message isRange [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".
