Problem overview
The function longWords receives an array of strings, words, and an integer n. It returns a new array with the words that have more than n characters, in the order they had in words. If no word qualifies, the result is an empty array.
For words = ["apple", "kiwi", "banana", "fig"] and n = 4, the lengths are 5, 4, 6 and 3. Only "apple" and "banana" are longer than 4, so the result is ["apple", "banana"]. "kiwi" has exactly 4 characters, which is not more than 4, so it is left out.
Approach: collect what passes the test
This is the basic filtering pattern: start with an empty array, look at each item once, and append the ones that meet a condition. Because the loop visits the words from first to last and each match goes to the end of result, the original order is kept without any extra work.
The condition is a strict comparison. A word of exactly n characters fails word.count > n, which is what the problem asks for. Writing >= would keep it by mistake.
Step by step
- Create
resultas an empty[String]declared withvar. It has to be a variable because it grows inside the loop. - Loop over
wordswithfor word in words, so every word gets a turn. - Test the length with
word.count > n. - Append the word inside the
if, so only the long ones are added. - Return
resultafter the loop. If nothing matched, it is still empty, which is the right answer.
Tracing the first example with n = 4: "apple" has 5 characters, 5 > 4, so it is appended. "kiwi" has 4, and 4 > 4 is false, so it is skipped. "banana" (6) is appended and "fig" (3) is skipped. The loop ends with result holding "apple" and "banana", in that order, and the function returns it.
The same loop works for any condition. Change the test to word.count < n, or to a test on the first letter, and you get a different filter with the same shape.
The solution in Swift
class Solution {
func longWords(_ words: [String], _ n: Int) -> [String] {
var result = [String]()
for word in words {
if word.count > n {
result.append(word)
}
}
return result
}
}
A few Swift details:
[String]()creates an empty array of strings. Writingvar result: [String] = []means the same thing.countcounts characters. AStringin Swift is a collection ofCharactervalues, andword.countis how many of them it holds. A character is what a reader sees as one symbol, so"café".countis 4 even when the accent is stored as a separate combining mark. The lesson's words are plain lowercase letters, so the number is simply the letter count.- Value types. Arrays are values in Swift.
wordsis never changed; the function builds and returns a separate array.
Edge cases and common mistakes
- Using
>=. Withn = 4, "kiwi" would be wrongly included. - Nothing long enough.
["hi", "to", "be"]withn = 3returns[]. - Repeated words.
["go", "went", "gone", "went"]withn = 3returns["went", "gone", "went"]: duplicates are kept, each in its own position. n = 0. Every word has at least one letter, so every word is returned:["ab"]withn = 0gives["ab"].- Returning inside the loop. A
returnin theifwould stop at the first long word. The return belongs after the loop.
Testing
["apple", "kiwi", "banana", "fig"],n = 4gives["apple", "banana"].["hi", "to", "be"],n = 3gives[].["go", "went", "gone", "went"],n = 3gives["went", "gone", "went"].["ab"],n = 0gives["ab"].
