Longest Word in Sentence

Easy~10 min

Given a string s consisting of words separated by spaces, return the longest word in the sentence.

If there are multiple words with the same maximum length, return the first one that appears.

A word is defined as a sequence of non-space characters.

Examples

Example 1
Input: s = "the quick brown fox"
Output: "quick"
Explanation: "quick" and "brown" both have 5 characters, but "quick" appears first.
Example 2
Input: s = "hello"
Output: "hello"
Explanation: There is only one word in the sentence.
Example 3
Input: s = "I love programming"
Output: "programming"
Explanation: "programming" has 11 characters, which is the longest.

Constraints

  • 1 <= s.length <= 10^4
  • s contains only lowercase English letters and spaces
  • s does not contain leading or trailing spaces
  • Words are separated by a single space
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run