Given an array of strings arr[], where each arr[i] represents a single sentence with no leading or trailing spaces. Return the count of the maximum number of words that appear in a single string.
Input: arr[] = {“Welcome to geeks for geeks”, “Happy Coding”, “We love gfg”}
Output: 5
Explanation: The length of arr[0], arr[1] & arr[2] is 5, 2, 3. Among them, 5 is the maximum.Input: arr[] = {“Start your coding journey”, “Ready to code”}
Output: 4
Approach: This can be solved by the following idea:
By iterating through the whole string and maintaining maximum words in each string.
Steps involved in the implementation of code:
- We have to iterate each string from arr.
- Then find out the length of the strings & compare them with other strings.
- After that compare the length with the other string’s length & return it.
Below is the implementation of the code:
#include <iostream> #include <string> using namespace std;
// Function that return the max length int countMaxLength(string arr[], int n)
{ // Hold the max length
int max = 0;
for ( int i = 0; i < n; i++) {
// Creating a array of arr[i]
// which is a string
string delimiter = " " ;
size_t pos = 0;
int len = 0;
string sub;
string str = arr[i];
while ((pos = str.find(delimiter))
!= string::npos) {
sub = str.substr(0, pos);
str.erase(0, pos + delimiter.length());
len++;
}
len++;
// Comparing the length
if (max < len)
max = len;
}
// Return max words in string
return max;
} // Driver code int main()
{ string arr[] = { "Welcome to geeks for geeks" ,
"Happy Coding" , "We love gfg" };
int n = sizeof (arr) / sizeof (arr[0]);
// Function call
int ans = countMaxLength(arr, n);
cout << ans << endl;
return 0;
} // This code is contributed by Susobhan Akhuli |
/* java program to find max length of a word in arr */ import java.io.*;
class GFG {
// Function that return the max length
public static int countMaxLength(String arr[])
{
// Length of the array
int n = arr.length;
// Hold the max length
int max = 0 ;
for ( int i = 0 ; i < n; i++) {
// Creating a array of arr[i]
// which is a string
String sub[] = arr[i].split( " " );
int len = sub.length;
// Comparing the length
max = Math.max(max, len);
}
// Return max words in string
return max;
}
// Driver code
public static void main(String[] args)
{
String arr[] = { "Welcome to geeks for geeks" ,
"Happy Coding" , "We love gfg" };
// Function call
int ans = countMaxLength(arr);
System.out.print(ans);
}
} |
# Python3 program to find max length # of a word in arr # Function that returns the max length def countMaxLength(arr, n):
# Hold the max length
max_len = 0
for i in range (n):
# Creating a list of words from arr[i] string
words = arr[i].split()
# Counting the number of words in the string
len_words = len (words)
# Comparing the length
if max_len < len_words:
max_len = len_words
# Return max words in string
return max_len
# Driver code if __name__ = = "__main__" :
arr = [ "Welcome to geeks for geeks" ,
"Happy Coding" , "We love gfg" ]
n = len (arr)
# Function call
ans = countMaxLength(arr, n)
print (ans)
# This code is contributed by Prajwal Kandekar |
using System;
class Program
{ // Function that return the max length
static int CountMaxLength( string [] arr, int n)
{
// Hold the max length
int max = 0;
for ( int i = 0; i < n; i++)
{
// Creating a array of arr[i]
// which is a string
string delimiter = " " ;
int len = 0;
string str = arr[i];
while (str.Contains(delimiter))
{
int pos = str.IndexOf(delimiter);
string sub = str.Substring(0, pos);
str = str.Substring(pos + delimiter.Length);
len++;
}
len++;
// Comparing the length
if (max < len)
max = len;
}
// Return max words in string
return max;
}
// Driver code
static void Main( string [] args)
{
string [] arr = { "Welcome to geeks for geeks" ,
"Happy Coding" , "We love gfg" };
int n = arr.Length;
// Function call
int ans = CountMaxLength(arr, n);
Console.WriteLine(ans);
}
} |
// JavaScript code for the approach // Function that return the max length function countMaxLength(arr) {
// Hold the max length
let max = 0;
for (let i = 0; i < arr.length; i++) {
// Creating a array of arr[i] which is a string
let delimiter = " " ;
let len = 0;
let str = arr[i];
let pos = str.indexOf(delimiter);
while (pos !== -1) {
let sub = str.substring(0, pos);
str = str.substring(pos + delimiter.length);
len++;
pos = str.indexOf(delimiter);
}
len++;
// Comparing the length
if (max < len) {
max = len;
}
}
// Return max words in string
return max;
} // Driver code let arr = [ "Welcome to geeks for geeks" , "Happy Coding" , "We love gfg" ];
let ans = countMaxLength(arr); console.log(ans); |
5
Time Complexity: O(n*len)
Auxiliary Space: O(len)