Find the first maximum length even word from a string
Last Updated :
07 May, 2025
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”.
Examples:
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:
Approach to solve this problem is to split the string into words, and then iterate over each word to check if it has even length and is longer than the current maximum length even word found so far. If a suitable word is found, update the maximum length even word.
Here are the steps of the approach:
- Initialize a variable maxLenEvenWord to an empty string.
- Split the input string str into words using stringstream and getline.
- For each word w in the list of words, If the length of w is even and greater than the length of maxLenEvenWord, update maxLenEvenWord to w.
- Return maxLenEvenWord.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string findMaxLenEven(string str) {
string maxLenEvenWord = "";
stringstream ss(str);
string word;
while (getline(ss, word, ' ')) {
if (word.length() % 2 == 0 && word.length() > maxLenEvenWord.length()) {
maxLenEvenWord = word;
}
}
return maxLenEvenWord;
}
int main() {
string str = "this is a test string";
cout << findMaxLenEven(str) << endl;
return 0;
}
Java
import java.util.StringTokenizer;
public class GFG {
// Function to find the word with the maximum even length
public static String findMaxLenEven(String str) {
String maxLenEvenWord = "";
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) {
String word = st.nextToken();
if (word.length() % 2 == 0 && word.length() > maxLenEvenWord.length()) {
maxLenEvenWord = word;
}
}
return maxLenEvenWord;
}
public static void main(String[] args) {
String str = "this is a test string";
System.out.println(findMaxLenEven(str));
}
}
Python
# Define a function that takes a string as input and
# returns the longest even length word in the string
def findMaxLenEven(str):
# Initialize an empty string to store the longest even length word
maxLenEvenWord = ""
# Split the input string into a list of words
words = str.split()
# Iterate over each word in the list
for word in words:
# Check if the length of the word is even and greater than
# the length of the current longest even length word
if len(word) % 2 == 0 and len(word) > len(maxLenEvenWord):
# If so, update the current longest even length word
maxLenEvenWord = word
# Return the longest even length word
return maxLenEvenWord
# Define a string to test the function
str = "this is a test string"
# Call the function and print the result
print(findMaxLenEven(str))
C#
using System;
public class GFG {
// Function to find the word with the maximum even length
public static string FindMaxLenEven(string str) {
string maxLenEvenWord = "";
string[] words = str.Split(' ');
foreach (string word in words) {
if (word.Length % 2 == 0 && word.Length > maxLenEvenWord.Length) {
maxLenEvenWord = word;
}
}
return maxLenEvenWord;
}
public static void Main(string[] args) {
string str = "this is a test string";
Console.WriteLine(FindMaxLenEven(str));
}
}
JavaScript
// Define a function that takes a string as input and
// returns the longest even length word in the string
function findMaxLenEven(str) {
// Initialize an empty string to store the longest even length word
let maxLenEvenWord = "";
// Split the input string into a list of words
let words = str.split(" ");
for (let i = 0; i < words.length; i++) {
let word = words[i];
if (word.length % 2 === 0 && word.length > maxLenEvenWord.length) {
maxLenEvenWord = word;
}
}
// Return the longest even length word
return maxLenEvenWord;
}
// Define a string to test the function
const str = "this is a test string";
// Call the function and print the result
console.log(findMaxLenEven(str));
Time Complexity: O(N+M) time, where N is the length of the input string. We iterate over each word once, and for each word, we perform a constant number of operations (length check and update), so the overall time complexity of the loop is O(M), where M is the total number of words in the input string. total time complexity of the algorithm is O(N+M).
Space Complexity: O(N), We need to store the input string and the maximum length even word, which takes O(N) space, where N is the length of the input string.
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:
C++
// C++ program to find maximum length even word
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum length even word
string findMaxLenEven(string str)
{
int n = str.length();
int i = 0;
// To store length of current word.
int currlen = 0;
// To store length of maximum length word.
int maxlen = 0;
// To store starting index of maximum
// length word.
int 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++;
}
i++;
}
// Check length of last word.
if (currlen % 2 == 0) {
if (maxlen < currlen) {
maxlen = currlen;
st = i - currlen;
}
}
// If no even length word is present
// then return -1.
if (st == -1)
return "-1";
return str.substr(st, maxlen);
}
// Driver code
int main()
{
string str = "this is a test string";
cout << findMaxLenEven(str);
return 0;
}
Java
// Java program to find maximum length even word
class GFG
{
// Function to find maximum length even word
static String findMaxLenEven(String str)
{
int n = str.length();
int i = 0;
// To store length of current word.
int currlen = 0;
// To store length of maximum length word.
int maxlen = 0;
// To store starting index of maximum
// length word.
int 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.charAt(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++;
}
i++;
}
// Check length of last word.
if (currlen % 2 == 0)
{
if (maxlen < currlen)
{
maxlen = currlen;
st = i - currlen;
}
}
// If no even length word is present
// then return -1.
if (st == -1)
return "-1";
return str.substring(st, st + maxlen);
}
// Driver code
public static void main(String args[])
{
String str = "this is a test string";
System.out.println( findMaxLenEven(str));
}
}
// This code is contributed by Arnab Kundu
Python
# Python3 program to find maximum
# length even word
# Function to find maximum length
# even word
def findMaxLenEven(str):
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 length 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
C#
// C# program to find maximum length even word
using System;
class GFG
{
// Function to find maximum length even word
static String findMaxLenEven(string str)
{
int n = str.Length;
int i = 0;
// To store length of current word.
int currlen = 0;
// To store length of maximum length word.
int maxlen = 0;
// To store starting index of maximum
// length word.
int 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++;
}
i++;
}
// Check length of last word.
if (currlen % 2 == 0)
{
if (maxlen < currlen)
{
maxlen = currlen;
st = i - currlen;
}
}
// If no even length word is present
// then return -1.
if (st == -1)
return "-1";
return str.Substring(st, maxlen);
}
// Driver code
public static void Main()
{
string str = "this is a test string";
Console.WriteLine(findMaxLenEven(str));
}
// This code is contributed by Ryuga
}
JavaScript
<script>
// Javascript program to find maximum length even word
// Function to find maximum length even word
function findMaxLenEven(str)
{
var n = str.length;
var i = 0;
// To store length of current word.
var currlen = 0;
// To store length of maximum length word.
var maxlen = 0;
// To store starting index of maximum
// length word.
var 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++;
}
i++;
}
// Check length of last word.
if (currlen % 2 == 0) {
if (maxlen < currlen) {
maxlen = currlen;
st = i - currlen;
}
}
// If no even length word is present
// then return -1.
if (st == -1)
return "-1";
return str.substr(st, maxlen);
}
// Driver code
var str = "this is a test string";
document.write( findMaxLenEven(str));
// This code is contributed by noob2000.
</script>
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Similar Reads
Program to find Smallest and Largest Word in a String Given a string, find the minimum and the maximum length words in it. Examples: Input : "This is a test string"Output : Minimum length word: a Maximum length word: stringInput : "GeeksforGeeks A computer Science portal for Geeks"Output : Minimum length word: A Maximum length word: GeeksforGeeksMethod
15+ min read
Length Of Last Word in a String Given a string s consisting of upper/lower-case alphabets and empty space characters ' ', return the length of the last word in the string. If the last word does not exist, return 0. Examples: Input : str = "Geeks For Geeks"Output : 5length(Geeks)= 5Input : str = "Start Coding Here"Output : 4length(
13 min read
Maximum length substring with highest frequency in a string Given a string. The task is to find the maximum occurred substring with a maximum length. These occurrences can overlap. Examples: Input: str = "abab" Output: ab "a", "b", "ab" are occur 2 times. But, "ab" has maximum length Input: str = "abcd" Output: a Approach: The idea is to store the frequency
5 min read
Print all strings of maximum length from an array of strings Given an array of strings arr[], the task is to print all the strings of maximum length from the given array. Example: Input: arr[] = {âabaâ, âaaâ, âadâ, âvcdâ, âabaâ}Output: aba vcd abaExplanation:Maximum length among all the strings from the given array is 3.The strings having length equal to 3 fr
7 min read
Substring of length K having maximum frequency in the given string Given a string str, the task is to find the substring of length K which occurs the maximum number of times. If more than one string occurs maximum number of times, then print the lexicographically smallest substring. Examples: Input: str = "bbbbbaaaaabbabababa", K = 5Output: ababaExplanation:The sub
14 min read