Problem overview
The method longWords receives an array of strings, words, and an integer n. It returns a List<String> holding the words that have more than n characters, in the order they had in the array.
With ["apple", "kiwi", "banana", "fig"] and n = 4 the result is [apple, banana]. "kiwi" has exactly 4 characters, and the rule is strictly longer, so it is left out. With ["hi", "to", "be"] and n = 3 no word qualifies and the result is the empty list [].
Approach: collect the words that pass a test
This is a filter. You look at each word once, ask one yes/no question, and keep the word only when the answer is yes. You don't know in advance how many words will pass, so an array, whose size is fixed when it is created, is a poor fit for the answer. A list grows as you add to it, which is why the method returns a List.
Step by step
- Create the result list.
List<String> result = new ArrayList<>();starts empty. The variable's type is the interfaceList, the object is anArrayList, and the empty<>lets the compiler fill inString. - Visit every word with an enhanced
forloop. Each pass gives you one element of the array in the variableword, from the first to the last. - Test the length. A
Stringreports its number of characters withword.length(), with parentheses, unlike an array'slengthfield. The condition isword.length() > n. - Add the word to
resultinside theif.addappends at the end, and the loop meets the words in their original order, so the order is preserved without any extra work. - Return the list after the loop. If nothing passed,
resultis still empty, and an empty list is the right answer.
The solution in Java
class Solution {
public List<String> longWords(String[] words, int n) {
List<String> result = new ArrayList<>();
for (String word : words) {
if (word.length() > n) {
result.add(word);
}
}
return result;
}
}
A few Java details:
length()againstlength. Strings use a method, arrays use a field:words.lengthwould be the number of words,word.length()is the number of characters.- Printing a list. A list prints in square brackets, with elements separated by a comma and a space and no quotes, so the first example prints as
[apple, banana]and the second as[]. - Duplicates stay. A list keeps every element you add. With
["go", "went", "gone", "went"]andn = 3the result is[went, gone, went]: the repeated word appears twice. - Imports.
ListandArrayListbelong to thejava.utilpackage, so a standalone program needsimport java.util.*;at the top.
Edge cases and common mistakes
- Using
>=. That would keep words of exactlyncharacters. In the first example "kiwi" would wrongly appear in the result. n = 0. Every word has at least one letter, so every word is kept:["a", "bb"]withn = 0gives[a, bb].- Returning inside the loop. A
returnthere would end the method after the first word. The return belongs after the loop. - Returning
nullfor "nothing found". The lesson wants an empty list, which callers can loop over safely.
Testing
["apple", "kiwi", "banana", "fig"]withn = 4gives[apple, banana].["hi", "to", "be"]withn = 3gives[].["go", "went", "gone", "went"]withn = 3gives[went, gone, went].["a", "bb"]withn = 0gives[a, bb].
