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
- 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. - Add the hyphen as the string
"-". - 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 aStringand single quotes make aChar. ACharin 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 aStringIndexOutOfBoundsException. The lesson's constraints exclude this case. - Changing case. The first two characters are copied unchanged, so
"Hello"starts with an upper-caseHboth times.
Testing
"dog"gives"do-dog"."Hello"gives"He-Hello"."ab"gives"ab-ab".
