Given an ArrayList of strings words, return the longest word: the one with the most letters.
If several words share the longest length, return the first of them.
Example 1:
Input: words = ["I", "love", "pancakes"] Output: "pancakes" Explanation: "pancakes" has 8 letters, more than "I" (1) and "love" (4).
Example 2:
Input: words = ["apple", "kiwi", "mango"] Output: "apple" Explanation: "apple" and "mango" both have 5 letters, the most. "apple" comes first, so it is the answer.
Example 3:
Input: words = ["code"] Output: "code" Explanation: With only one word, it is the longest.
Constraints:
wordshas at least one word.- Every word is made of English letters, lowercase or uppercase.
- On a tie, the answer is the first of the longest words.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
