Problem overview
The function longWords receives an array of strings, words, and an Int, n. It returns a list with the words that have more than n characters, in the order they had in words. A word with exactly n characters is left out, and when no word is long enough the result is an empty list.
For words = ["apple", "kiwi", "banana", "fig"] and n = 4, the result is [apple, banana]. kiwi has exactly 4 characters, so it fails the test, and fig has only 3.
Approach: collect what passes
The result can have any length from zero to words.size, so it can't be a fixed-size array created up front. A growing list fits: start with an empty one, look at each word once, and append the word when it passes the test. Because the loop visits the words from first to last and each one is appended at the end, the order of words is kept without any extra work.
Step by step
- Create the empty list.
mutableListOf<String>()makes a list that can grow, and the type argument says it holds strings. Store it in aval: the variable will always refer to this one list, and that is fine, because adding items changes the list, not the variable. - Loop over the words.
for (word in words)gives each element of the array in turn. - Test the length.
word.length > nis true only for words strictly longer thann. With>=,kiwiwould be kept in the first example. - Add the passing word to the list inside the
if. - Return the list after the loop, not inside it. If nothing passed, it is still empty, and an empty list is the right answer.
The solution in Kotlin
class Solution {
fun longWords(words: Array<String>, n: Int): List<String> {
val result = mutableListOf<String>()
for (word in words) {
if (word.length > n) {
result.add(word)
}
}
return result
}
}
Kotlin details worth knowing:
- Mutable inside, read-only outside.
mutableListOfreturns aMutableList, which is also aList, so returning it matches the declared return typeList<String>. The caller sees a list it can read but not change through that type. valversusvar.val resultcan't be pointed at another list later.result.add(word)never does that, so novaris needed.- Array versus list. The parameter is an
Array<String>, and the result is aList<String>. Theforloop reads either one in the same way. - Length counts characters of the string.
word.lengthis the number of UTF-16 units in the string, which for lowercase English letters is the number of letters.
Edge cases and common mistakes
- Exactly
ncharacters. With["kiwi"]andn = 4the result is empty, because the rule is "longer than", not "at least". n = 0. Every word has at least one letter, so every word is kept.- Repeated words.
["go", "went", "gone", "went"]withn = 3gives[went, gone, went]: nothing removes duplicates, and each occurrence is tested on its own. - Returning too early. A
returninside the loop would stop at the first long word.
Testing
["apple", "kiwi", "banana", "fig"]withn = 4gives[apple, banana].["hi", "to", "be"]withn = 3gives[].["go", "went", "gone", "went"]withn = 3gives[went, gone, went].["cat", "dog"]withn = 0gives[cat, dog].
