Problem overview
The function longWords receives a vector<string> named words and an int named n. It returns a new vector with the words that have more than n characters, in their original order. A word with exactly n characters is left out, and when no word qualifies, the result is an empty vector.
With words = ["apple", "kiwi", "banana", "fig"] and n = 4, the function returns ["apple", "banana"]. "kiwi" has exactly 4 characters, so it fails the "strictly longer" test. With ["hi", "to", "be"] and n = 3 the result is [], and with ["go", "went", "gone", "went"] and n = 3 it is ["went", "gone", "went"]: a repeated word is kept every time it qualifies.
Approach: filter into a new vector
This is the filter pattern: walk through the input once, test each element, and copy the ones that pass into a result collection. The input stays untouched. Because the loop visits the words from first to last and each passing word is added at the end of the result, the order is preserved with no extra work.
Step by step
- Create the result. A
vector<string>declared without values starts empty. It will hold the words that pass. - Loop over the words. A range-based
forhands the loop one word per turn, in order. - Test the length.
word.size() > nis true only for words with more thanncharacters. Using>=would keep words of exactlyncharacters, which the task excludes. - Add the word.
push_backappends it to the end ofresult. - Return after the loop. If nothing was added,
resultis still empty, which is the correct answer.
The solution in C++
class Solution {
public:
vector<string> longWords(vector<string> &words, int n) {
vector<string> result;
for (string word : words) {
if (word.size() > n) {
result.push_back(word);
}
}
return result;
}
};
The C++ details worth knowing:
size()is unsigned.word.size()returns an unsigned type, whilenis a signedint. When the two meet in a comparison, the signed value is converted to unsigned. That is harmless here becausenis never negative, but with a negative number it would turn into a huge value:word.size() > -1is false for every word, since-1becomes the largest unsigned number. A compiler may warn about this comparison.for (string word : words)copies. Eachwordis a copy of the element. That is fine for short words. For long strings,for (const string &word : words)reads the elements without copying them.wordsis passed by reference. The&in the parameter avoids copying the whole vector when the function is called. The function doesn't change it.- Returning a vector is cheap. Returning
resultby value does not copy every string: the compiler moves it or builds it in place.
Edge cases and common mistakes
>=instead of>. For the first example withn = 4, it would wrongly include "kiwi".n = 0. Every word has at least one letter, so every word is kept.- Returning inside the loop. A
returninside theifwould send back after the first match instead of collecting them all. - Repeated words. They are not removed: each occurrence is tested and added on its own.
Testing
["apple", "kiwi", "banana", "fig"]withn = 4gives["apple", "banana"].["hi", "to", "be"]withn = 3gives[].["go", "went", "gone", "went"]withn = 3gives["went", "gone", "went"].
