First Longest Even Word

Easy
0/40
0 upvote
Asked in company
FactSet

Problem statement

You are given a string s containing words separated by spaces. Your task is to find and return the first word in the string that satisfies two conditions:


1) The word must have an even length.
2) The word's length must be the maximum among all other even-length words in the string.


If multiple words of the same maximum even length exist, you must return the one that appears first in the original string.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
A single line containing the string s.


Output Format:
Print a single string which is the first longest even-length word.

If no even-length words are found in the string, print "00".


Note:
A word is a sequence of non-space characters.

Multiple spaces between words, as well as leading/trailing spaces, should be handled correctly.
Sample Input 1:
You are given an array of n numbers


Sample Output 1:
an


Explanation for Sample 1:
The even-length words in the string are "an" (length 2) and "of" (length 2).
The maximum even length is 2.
Since both "an" and "of" have this length, we choose the one that appears first, which is "an".


Sample Input 2:
odd words only


Sample Output 2:
00


Explanation for Sample 2:
All words ("odd", "words", "three") have odd lengths. Since no even-length words exist, the output is "00".


Expected Time Complexity:
The expected time complexity is O(N), where N is the length of the string.


Constraints:
1 <= length of s <= 10^5
The string consists of lowercase English letters and spaces.

Time limit: 1 sec

Approaches (1)
First Longest Even Word
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
First Longest Even Word
Full screen
Console