Problem 101670 · easy · Phase 01 Prerequisites & Setup

Longest Word Wins

strings · split · len · iteration

Given a sentence s, return the longest word. If several words share the greatest length, return the one that appears first. Words are separated by spaces and punctuation attached to a word counts as part of it. Return "" if there are no words.

Examples

Input:  s = "I love programming in Python"
Output: "programming"

Input:  s = "red blue green"
Output: "green"

Constraints

  • 0 <= len(s) <= 5000
  • s may contain letters, digits, spaces and punctuation

Goals

  • Iterate over words and track the best so far
  • Break ties by keeping the earliest word
Starting Python…