Problem overview
The function wordLengths takes a vector<string> named words and returns a vector<int> with the number of letters of each word, in the same order. For ["I", "love", "pancakes"] the answer is [1, 4, 8]. For ["banana", "kiwi", "fig"] it is [6, 4, 3], and for the single word ["code"] it is [4].
Approach: one result per input
This is the transform pattern: every input element produces exactly one output element. You create an empty result, visit each word once, and append that word's length. The result is as long as words, and because you append in the order you visit, the lengths line up with the words.
Compare this with the filter pattern, where only some elements make it into the result. Here nothing is skipped: there is no if, and the loop body is a single statement. If you can say "one answer per element", this is the shape to reach for.
Step by step
- Create
lengths. It is avector<int>, and it starts empty because no length has been computed yet. - Loop over
words. A range-basedforgives the loop onestringnamedwordper turn. - Append the length.
word.size()is the number of characters, andpush_backputs it at the end oflengths. - Return after the loop. Returning inside the loop would send back a vector with only the first length.
The solution in C++
class Solution {
public:
vector<int> wordLengths(vector<string> &words) {
vector<int> lengths;
for (string word : words) {
lengths.push_back(word.size());
}
return lengths;
}
};
The C++ details worth knowing:
size()returns an unsigned number. Its type issize_t, andpush_backinto avector<int>converts it tointsilently. Word lengths are far below theintlimit, so nothing is lost.size()counts bytes. For the English letters in this lesson, one letter is one byte, so the count is the number of letters. Text with accented or other non-ASCII characters can use several bytes per character in astring.for (string word : words)copies each word. The loop only needssize(), sofor (const string &word : words)would give the same result without the copies.- The vector grows as needed.
push_backtakes care of the memory; you never declare a size.
Reading the result
The returned vector can be used like any other. Its size() equals the number of words, and lengths[0] is the length of the first word. For ["I", "love", "pancakes"], lengths[2] is 8. Because the order is kept, position i of the result always describes position i of words, which is what lets you pair them up later. If a word list is empty, the loop never runs and an empty vector comes back, although this lesson always gives at least one word.
Edge cases and common mistakes
- Returning too early. Put
return lengths;after the closing brace of the loop. - Using
lengths[i] = ...on an empty vector. Indexing a vector doesn't add elements, so writing past its end is undefined behaviour. Usepush_backto add. - Uppercase letters. They count like lowercase ones: the length of "Hello" is 5.
- One word.
["code"]gives a vector with a single element,[4].
Testing
["I", "love", "pancakes"]gives[1, 4, 8].["banana", "kiwi", "fig"]gives[6, 4, 3].["code"]gives[4].
