Given a string of words separated by spaces. The task is to find the first maximum length even word from the string. Eg: “You are given an array of n numbers” The answer would be “an” and not “of” because “an” comes before “of”.
Input: "this is a test string" Output: string Even length words are this, is, test, string. Even maximum length word is string. Input: "geeksforgeeks is a platform for geeks" Output: platform Only even length word is platform.
Approach: The idea is to traverse the input string and find length of each word. Check if the length of word is even or not. If even, then compare length with maximum length found so far. If length is strictly greater than maximum length then store current word as required string.
Below is the implementation of above approach:
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C program to find and replace a word in a File by another given word
- Find the word from a given sentence having given word as prefix
- String containing first letter of every word in a given string with spaces
- Most frequent word in first String which is not present in second String
- Longest Common Prefix using Word by Word Matching
- C program to Replace a word in a text by another given word
- Find the first repeated word in a string
- Get the first letter of each word in a string using regex in Java
- Print the first and last character of each word in a String
- Remove the first and last character of each word in a string
- Capitalize the first and last character of each word in a string
- Reverse every word of the string except the first and the last character
- First substring whose reverse is a word in the string
- Length Of Last Word in a String
- Maximum even length sub-string that is permutation of a palindrome
- Longest Even Length Substring such that Sum of First and Second Half is same
- Sum of first K even-length Palindrome numbers
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Check if a string contains a palindromic sub-string of even length
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.