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:
# Python3 program to find maximum
# length even word
# Function to find maximum length
# even word
n = len(str)
i = 0
# To store length of current word.
currlen = 0
# To store length of maximum length word.
maxlen = 0
# To store starting index of maximum
# length word.
st = -1
while (i < n): # If current character is space then # word has ended. Check if it is even # length word or not. If yes then # compare length with maximum length # found so far. if (str[i] == ' '): if (currlen % 2 == 0): if (maxlen < currlen): maxlen = currlen st = i - currlen # Set currlen to zero for next word. currlen = 0 else : # Update length of current word. currlen += 1 i += 1 # Check length of last word. if (currlen % 2 == 0): if (maxlen < currlen): maxlen = currlen st = i - currlen # If no even lengh word is present # then return -1. if (st == -1): print("trie") return "-1" return str[st: st + maxlen] # Driver code if __name__ == "__main__": str = "this is a test string" print(findMaxLenEven(str)) # This code is contributed by Ita_c [tabby title = "C#"]
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
- Length Of Last Word in a String
- Find the first repeated word in a string
- Find frequency of each word in a string in Python
- Program to find Smallest and Largest Word in a String
- Count maximum-length palindromes in a String
- Maximum even length sub-string that is permutation of a palindrome
- Find length of longest subsequence of one string which is substring of another string
- Find largest word in dictionary by deleting some characters of given string
- Maximum length prefix of one string that occurs as subsequence in another
- Maximum length substring with highest frequency in a string
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Length of the smallest sub-string consisting of maximum distinct characters
- Maximum length of balanced string after swapping and removal of characters
- Program for length of the longest word in a sentence
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
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.