Problem overview
The function wordLengths receives a list of strings, words, and returns a list of integers: the length of each word, in the same order. For ["I", "love", "pancakes"] the result is [1, 4, 8], one number per word.
Approach: one number out for each word in
The result has exactly as many items as words, and the item at each position depends only on the word at the same position. That makes it a loop with a list that grows: start empty, and for every word add its length at the end. Adding at the end keeps the order.
Step by step
- Make an empty list of
IntwithmutableListOf<Int>()and store it inlengths. Thevalkeyword means the name always refers to this list. It doesn't stop the list from growing. - Loop over the words with
for (word in words). The loop body runs once per word. - Add the length.
word.lengthis the number of characters in the string, andlengths.add(...)appends it. - Return
lengthsafter the loop. Areturninside the loop would send back after the first word only.
The solution in Kotlin
class Solution {
fun wordLengths(words: List<String>): List<Int> {
val lengths = mutableListOf<Int>()
for (word in words) {
lengths.add(word.length)
}
return lengths
}
}
Kotlin details worth knowing:
- The type argument matters.
mutableListOf<Int>()with nothing inside gives Kotlin no values to infer the element type from, so you write it yourself. Without<Int>the call doesn't compile. lengthis a property. Writeword.length, with no parentheses. A string's size in Kotlin islength, while a list's size issize.- Return type. The function declares
List<Int>and returns aMutableList<Int>. That is allowed because every mutable list is also a list. - Boxed numbers. A
List<Int>holds boxed integers on the JVM. For this size of task that makes no practical difference.
Tracing the loop
For ["I", "love", "pancakes"], the list after each word looks like this:
I -> [1]
love -> [1, 4]
pancakes -> [1, 4, 8]
Each pass adds exactly one number at the end, so after the loop the list has as many items as there are words. This one-in, one-out shape is common enough that Kotlin has a built-in for it, words.map { it.length }. Writing the loop first shows what map does under the hood.
Edge cases and common mistakes
- Uppercase letters.
"Hi"has length 2, like"hi". Length counts characters, whatever their case. - An empty string.
""has length 0, and that 0 still goes into the result:["Hi", ""]gives[2, 0]. - Keeping the order. Adding to the end of the list preserves the order of
words. Inserting at the front would reverse it. - Same length, different words. Nothing is merged: every word gets its own entry, even when two lengths are equal.
Using the result
The returned list always has the same size as words, which makes a quick self-check: if result.size differs from words.size, the loop added too often or too rarely. The numbers are plain Ints, so you can add them, compare them or pick the largest with the usual operators and functions.
Testing
["I", "love", "pancakes"]gives[1, 4, 8].["banana", "kiwi", "fig"]gives[6, 4, 3].["code"]gives[4].["Hi", ""]gives[2, 0].
