Problem overview
The function stutterize receives 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 is made of English letters only.
For "dog" the first two characters are do, so the result is "do-dog". For "Hello" it is "He-Hello": the case is kept, so the stutter starts with a capital H. For "ab" the first two characters are the whole word, and the result is "ab-ab".
Approach: three pieces joined together
The answer is built from three parts that never change order: the first two characters, the text "-" and the word itself. The only part that needs real work is the first. Swift strings can't be indexed with integers, so word[0] isn't available. The method prefix(2) does the job: it keeps the first two characters and doesn't need the length.
Step by step
- Take the first two characters with
word.prefix(2). - Convert them to a
String.prefixreturns aSubstring, and wrapping it inString(...)makes a string you can build on. - Add the hyphen as the string
"-". - Add the whole
wordand return the three pieces joined with+.
The solution in Swift
class Solution {
func stutterize(_ word: String) -> String {
return String(word.prefix(2)) + "-" + word
}
}
Details worth knowing:
- Why the conversion is there.
prefix(2)returns aSubstring, not aString. Swift can join aSubstringwith aStringusing+, but wrapping the piece inString(...)says plainly what type you are building, and it is the habit that matters when both sides are substrings:word.prefix(2) + word.suffix(1)would give aSubstring, which can't be returned from a function declared to return aString. - The word is used as it is. The third piece is the parameter itself, with no conversion: it is already a
String. - Short words are fine. If the word has fewer than two characters,
prefix(2)returns the whole word instead of crashing. The lesson guarantees two letters, so for"ab"the prefix is the entire word. - String joining.
+creates a new string each time. For a few pieces this is fine; for hundreds, building withappendor an interpolated string is a better habit.
String interpolation gives an equivalent version: "\(word.prefix(2))-\(word)". Interpolation turns the Substring into text automatically, so no String(...) is needed. The lesson's version uses + to practise joining.
Building the word piece by piece
Each part of the result has its own job, and you can see them one by one for "banana": the stutter is ba, the separator is -, and the word is banana, so the result is "ba-banana". The stutter is only ever as long as two characters, however long the word is, while the final part grows with the word. That is why the result of a 6-letter word has 9 characters and the result of "ab" has 5.
Edge cases and common mistakes
- Two-letter words.
"ab"gives"ab-ab". - Keeping the case. Don't change letters:
"Hello"gives"He-Hello", not"he-Hello". - Using
prefix(1). One character gives"d-dog", which is a different stutter. - Dropping the hyphen or adding spaces. The separator is exactly
-, with no spaces around it.
Testing
"dog"gives"do-dog"."Hello"gives"He-Hello"."ab"gives"ab-ab"."Be"gives"Be-Be"."banana"gives"ba-banana".
