AlgoMaster Logo

Find the Longest Word in a Sentence

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

The sentence is a string of words separated by spaces, and the answer is the word with the most characters, the word itself rather than its length or position.

If two words share the maximum length, return the one that appears earlier. Extra spaces need care, so a stray double space between words does not produce an empty word as the answer.

The problem combines two operations: separate the sentence into words, then compare those words by length. The tie rule falls out naturally if you only replace the current best when a word is strictly longer.

Key Constraints:

  • 1 <= s.length <= 1000 → The sentence has at least one word, so there is always an answer to return.
  • First word wins ties → When several words share the maximum length, the earliest one is returned.

Approach 1: Split and Compare

Intuition

Turn the sentence into a list of words first. The problem then becomes a single-pass search for the longest item, the same maximum scan you would run over a list of numbers. Track the best word so far, and replace it only when the current word is strictly longer. Using "strictly longer" rather than "longer or equal" makes the first of several equal-length words win.

Algorithm

  1. Split the sentence into words, treating any run of spaces as a separator so empty words never appear.
  2. Start with an empty string as the current best word.
  3. For each word, if its length is greater than the best length so far, make it the new best.
  4. After checking every word, return the best word.

Example Walkthrough

1Word "the" (length 3) becomes the best.
0
t
1
h
2
e
3
4
q
5
u
6
i
7
c
8
k
9
10
b
11
r
12
o
13
w
14
n
15
16
f
17
o
18
x
1/5

Take "the quick brown fox". The first word the has length 3, so it becomes the best. The next word quick has length 5, which is strictly longer, so it takes over as the best. The word brown also has length 5, but 5 is not strictly greater than 5, so the best stays quick. The last word fox has length 3, so nothing changes. The final answer is quick.

0
q
1
u
2
i
3
c
4
k
best

Code

Approach 2: Single Pass

Intuition

Splitting builds a whole collection of words just to discard most of them. Avoid that extra storage by scanning the sentence character by character.

Remember where the current word started and how long it has grown. When you reach a space or the end of the sentence, the word is finished, so compare its length against the best so far. If it is strictly longer, record its start position and length. This keeps the same first-wins tie behavior using only a few index variables.

Algorithm

  1. Track the start index and length of the current word, plus the best start index and best length found so far.
  2. Walk through the string one character at a time, going one step past the end so the final word is closed off.
  3. When you see a non-space character, extend the current word length, marking the start if it is the first character of the word.
  4. When you see a space or reach the end, the current word is complete, so update the best word if the current word is strictly longer, then reset the current word.
  5. After the scan, build the answer from the best start index and best length.

Example Walkthrough

1Finish word "a" (length 1). best = "a".
0
a
1
2
b
3
b
4
5
c
6
c
7
c
1/5

Take "a bb ccc". The scan finishes the word a at the first space, recording it as the best with length 1. It then finishes bb, which has length 2 and is strictly longer, so bb becomes the new best. The scan continues into ccc, growing the current length to 3. Reaching the end of the string closes that word, and since 3 beats the previous best of 2, ccc becomes the answer. No separate list of words is ever built. The word is finalized each time the scan crosses a space or reaches the end.

0
c
1
c
2
c
best

Code