Problem overview
The method stutterize receives a word of at least two letters and returns it said with a stutter: its first two characters, a hyphen, then the whole word. Letters keep their case.
For "dog" the first two characters are do, so the result is "do-dog". For "Hello" it is "He-Hello", with the upper-case H kept. For the two-letter word "ab", the first two characters are the whole word, so the result is "ab-ab".
Approach
The result has three pieces in a row: a slice of the word, the fixed text "-", and the word itself. The + operator joins strings, so the answer is those three pieces added together.
Notice that the word appears twice in the answer, once cut short and once whole. You don't need a loop or a helper for that. Python evaluates the slice, then the hyphen, then the word, and joins them left to right into one string. If the result looks wrong, check each piece on its own: print the slice, then print the whole expression.
Step by step
- Slice the first two characters.
word[:2]leaves out the start, so it begins at index0, and it stops before index2. That keeps indexes0and1. - Add the hyphen as the string
"-". - Add the whole word and return the result.
The solution in Python
class Solution:
def stutterize(self, word: str) -> str:
return word[:2] + "-" + word
Some details worth knowing:
- A slice stops before its end index, so
word[:2]has two characters, not three. - Strings are immutable, so each
+builds a new string. The originalwordis never changed, which is why it can appear twice in the result. - Because the word has at least two letters,
word[:2]always has exactly two characters. A shorter string would give a shorter slice rather than an error:"a"[:2]is"a".
Seeing the three pieces
Take word = "dog". The result is built from these parts, left to right:
word[:2]gives"do"."-"adds the hyphen.wordadds"dog".
Added together they make "do-dog". Changing the slice changes only the first piece: word[:1] would give "d-dog", and word[:3] would give "dog-dog". That is why the end index of the slice has to be exactly the number of characters the stutter repeats.
The same idea with a named variable
Naming the stutter makes the structure visible. For word = "Hello" this prints He, then He-Hello, then Hello:
word = "Hello"
start = word[:2]
print(start)
print(start + "-" + word)
print(word)
The last line confirms that word is untouched. The one-line solution does the same work without the variable, and either form passes the lesson's tests.
Edge cases and common mistakes
- Two-letter words.
"ab"gives"ab-ab": the slice and the word are the same text. - Using
word[:3]orword[:1]. The first gives"dog-dog"for"dog", the second"d-dog". Count the end index as the number of characters you keep. - Using
word[2]. A single index returns one character, the third one, not a two-character piece. - Changing case. The stutter keeps the word's case, so
"Hello"starts withHe, nothe. - Forgetting the hyphen, or typing a space or an underscore instead.
Testing
"dog"gives"do-dog"."Hello"gives"He-Hello"."ab"gives"ab-ab".
