Longest Word

Longest Word

Medium Python Strings 15 views
Explanation Complexity

Problem Statement

A sentence is provided. Words are separated by spaces. Output the longest word. If tie, output the first one.

Input Format

One line sentence.

Output Format

One word.

Example

I love programming so much
programming

Constraints

Length

Input / Output Format

Input Format
One line sentence.
Output Format
One word.
Constraints
Length

Examples

Input:
I love programming so much
Output:
programming

Example Solution (Public)

Python
import sys
s=sys.stdin.read()
if s is None: sys.exit(0)
if s.endswith('\
'):
  s=s[:-1]
words=[w for w in s.split(' ') if w!='']
best=''
for w in words:
  if len(w)>len(best):
    best=w
sys.stdout.write(best)

Official Solution Code

import sys
s=sys.stdin.read()
if s is None: sys.exit(0)
if s.endswith('\
'):
  s=s[:-1]
words=[w for w in s.split(' ') if w!='']
best=''
for w in words:
  if len(w)>len(best):
    best=w
sys.stdout.write(best)
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.