Longest Word Finder - Problem
Given a sentence as a string, find and return the longest word in the sentence.
If there are multiple words with the same maximum length, return the first occurrence.
Note: Words are separated by spaces, and you can assume the input contains only alphabetic characters and spaces.
Input & Output
Example 1 — Basic Case
$
Input:
sentence = "The quick brown fox jumps"
›
Output:
"brown"
💡 Note:
Words are ["The"(3), "quick"(5), "brown"(5), "fox"(3), "jumps"(5)]. Maximum length is 5. First word with length 5 is "brown".
Example 2 — Single Word
$
Input:
sentence = "Hello"
›
Output:
"Hello"
💡 Note:
Only one word exists, so "Hello" is the longest word.
Example 3 — All Equal Length
$
Input:
sentence = "cat dog pig"
›
Output:
"cat"
💡 Note:
All words have length 3. Return first occurrence "cat".
Constraints
- 1 ≤ sentence.length ≤ 104
- sentence contains only lowercase and uppercase English letters and spaces
- sentence does not start or end with a space
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code