Problem overview
The function longestWord receives a vector<string> named words and returns the word with the most letters. When several words share the greatest length, the first one wins. The vector always has at least one word.
For ["I", "love", "pancakes"] the answer is "pancakes". For ["apple", "kiwi", "mango"] it is "apple": "apple" and "mango" both have 5 letters, and "apple" comes first. For ["code"] it is "code".
Approach: keep the best seen so far
You don't need to sort the words or measure them all first. Keep a variable with the longest word seen so far, and give every word a chance to replace it. After the last word has had its turn, the variable holds the longest of all. This "best so far" pattern works for maximum, minimum and similar questions.
The starting value matters. Because the vector is never empty, the first word is a valid starting candidate: before looking at the others, it is the best you have.
Step by step
- Start with the first word.
string longest = words[0];gives the variable a real word to compare against. - Loop over the words. Each
wordis compared once with the current best. - Compare lengths with
>. A word replaceslongestonly if it is strictly longer. With>=, a later word of equal length would take over and break the tie rule. - Update the best. Inside the
if, assignwordtolongest. - Return after the loop. Every word has been compared by then.
The solution in C++
class Solution {
public:
string longestWord(vector<string> &words) {
string longest = words[0];
for (string word : words) {
if (word.size() > longest.size()) {
longest = word;
}
}
return longest;
}
};
The C++ details worth knowing:
words[0]is not checked. Indexing a vector doesn't verify the index. On an empty vector,words[0]is undefined behaviour. This is safe here only because the task promises at least one word.- Strings are copied by assignment.
longest = word;copies the characters, solongeststays valid and independent of the loop variable. - The first iteration compares the first word with itself. Its size is not greater than its own size, so nothing changes. It costs nothing and keeps the code simple.
- Both sides of
>are unsigned.size()returns an unsigned value for both strings, so there is no signed and unsigned mix here.
Complexity
Time: O(n), where n is the number of words (plus the cost of copying each word in the loop). Each word is visited once, and size() takes constant time. Space: O(L), where L is the length of the longest word, for the copy in longest and the loop variable.
Edge cases and common mistakes
- Ties.
["apple", "kiwi", "mango"]returns"apple"because>never replaces a word with an equal-length one. - A single word. The loop changes nothing and the first word is returned.
- Returning the length. The function returns the word, a
string, not its size. - Starting with an empty string. That works too, since any word is longer than
"", but starting withwords[0]is simpler to reason about.
Testing
["I", "love", "pancakes"]gives"pancakes".["apple", "kiwi", "mango"]gives"apple".["code"]gives"code".
