Problem overview
The function wordLengths takes an array of strings, words, and returns an array of integers: the length of each word, in the same order as the words. The result always has exactly one number per word.
For ["I", "love", "pancakes"] the answer is [1, 4, 8], because "I" has 1 letter, "love" has 4 and "pancakes" has 8.
Approach: one output for each input
Unlike filtering, nothing is skipped here. Each word produces one length, so the loop appends on every pass and the result ends up exactly as long as the input. Because the loop goes through the words in order and always appends at the end, position i of the result belongs to position i of words.
The length of a word is word.count. In Swift a String is a collection of characters, and count says how many it holds.
Step by step
- Start with an empty array of
Intnamedlengths, declared withvarbecause it grows. - Loop over the words with
for word in words. - Append
word.counttolengths. - Return
lengthsafter the loop. Areturninside the loop would hand back after the first word, with only one length in it.
Here is the loop on ["I", "love", "pancakes"]. lengths starts as []. After "I" it is [1], after "love" it is [1, 4], and after "pancakes" it is [1, 4, 8]. That final array is what the function returns.
The result has the same number of items as words, so the two arrays line up index by index: lengths[2] is the length of words[2]. That is useful when a later step needs both a word and its length.
The solution in Swift
class Solution {
func wordLengths(_ words: [String]) -> [Int] {
var lengths = [Int]()
for word in words {
lengths.append(word.count)
}
return lengths
}
}
Details worth knowing:
[Int]()is an empty array whose elements areInt. Swift needs to know the element type, and this spelling gives it.for word in wordsgives each element in turn as a constant. You can't changewordinside the loop, and you don't need to.- Uppercase and lowercase letters count the same. "I" and "i" both have length 1, so the constraint about letter case changes nothing.
- The shorter way. Swift's
mapturns an array into another array of the same length:words.map { $0.count }gives the same result. The lesson uses the explicit loop so the pattern of building a result is visible.
It is worth knowing what count measures. It counts characters as a reader sees them: the word "café" written with a combining accent (the letters c, a, f, e followed by an accent mark) still has a count of 4, although it takes 5 Unicode scalars and 6 bytes in UTF-8. The lesson's words use plain English letters, so you won't meet the difference here.
Edge cases and common mistakes
- One word.
["code"]gives[4]. - Order.
["banana", "kiwi", "fig"]gives[6, 4, 3], not a sorted list. The lengths are never rearranged. - Using
letforlengths. A constant array can't be appended to, and the compiler rejects theappendcall. - Returning too early. The
returnsits after the closing brace of the loop.
Testing
["I", "love", "pancakes"]gives[1, 4, 8].["banana", "kiwi", "fig"]gives[6, 4, 3].["code"]gives[4].
