First Longest Word with Even Character Count
StringsAuto-graded · 22 test casesLast updated
Tests string parsing, filtering by character parity, tracking maxima with tie-breaking, and handling whitespace edge cases.
You're given a string text made up of words separated by spaces. Find the word with the greatest number of characters where that count is an even number.
When multiple words share the maximum even length, return whichever appears earliest in the input. If the input contains no words with an even character count, return the string "00".
A word is any contiguous sequence of non-space characters.
Input
text: a string containing letters, digits, and spaces
Output
- A string: either the qualifying word, or
"00"if none exists
Constraints
1 <= len(text) <= 10^5- The input may have leading spaces, trailing spaces, or multiple consecutive spaces
- Words are composed of alphanumeric characters
Examples
Example 1
Input: first_longest_even_word('Time to write great code')
Output: 'Time'
Example 2
Input: first_longest_even_word('It is a pleasant day today')
Output: 'pleasant'
Example 3
Input: first_longest_even_word('You can do it')
Output: 'do'
Example 4
Input: first_longest_even_word('The red fox jumps')
Output: '00'
Hints
Hint 1
Split the input into tokens and filter for those with even lengths while tracking the longest one encountered so far.
Hint 2
Use a sentinel value to handle the case where no even-length word exists, avoiding special-case logic at the end.
Hint 3
When two words have the same even length, the first one wins—so only replace your current best when a new word is strictly longer, not equal.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Walk through the input once, examining each word to determine if it qualifies as a candidate. A word qualifies when its character count is even. Among all qualifying words, track the one with the greatest length, breaking ties by choosing the earliest occurrence.
Initialize a sentinel value "00" to represent "no valid word found yet." This simplifies the logic: you can return it directly if no even-length words exist, and checking against it tells you whether you've seen any candidates.
For each word extracted from the input:
- Skip empty strings (produced by extra spaces)
- Check if the length is even
- If this is the first valid candidate (
best == "00") or if it's strictly longer than the current best, replace the best
The strict inequality (> rather than >=) ensures that later words of equal length never displace earlier ones, implementing the tie-break rule correctly.
Splitting on a single space character produces empty tokens when spaces are consecutive, leading, or trailing. Guard against these by checking that the word is non-empty before evaluating its length.
Why It Works
Maintain this invariant: after processing the first k tokens, best holds the earliest longest even-length word among them, or remains "00" if none qualify.
Initially, no tokens have been seen, so best == "00" correctly represents the empty set of candidates.
When processing a new word:
- If it has odd length or is empty, the invariant is preserved (no change to
best) - If it has even length and is the first candidate, it becomes the new best
- If it's strictly longer than the current best, it's objectively better and replaces it
- If it's equal or shorter, the earlier word maintains its position
After the final token, the invariant guarantees best holds the correct answer.
Complexity
Time: O(n) where n is the length of the input. Splitting examines every character once, and each token is processed in constant time (length check and comparison).
Space: O(n) auxiliary space. The split operation creates a list of tokens whose combined length is proportional to the input size.
Solution Code
def first_longest_even_word(text: str) -> str:
best = "00"
for word in text.split(" "):
if word and len(word) % 2 == 0 and (best == "00" or len(word) > len(best)):
best = word
return best