Count maximum number of words in Array
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:
C++
#include <iostream>
#include <string>
using namespace std;
int countMaxLength(string arr[], int n)
{
int max = 0;
for ( int i = 0; i < n; i++) {
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++;
if (max < len)
max = len;
}
return max;
}
int main()
{
string arr[] = { "Welcome to geeks for geeks" ,
"Happy Coding" , "We love gfg" };
int n = sizeof (arr) / sizeof (arr[0]);
int ans = countMaxLength(arr, n);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int countMaxLength(String arr[])
{
int n = arr.length;
int max = 0 ;
for ( int i = 0 ; i < n; i++) {
String sub[] = arr[i].split( " " );
int len = sub.length;
max = Math.max(max, len);
}
return max;
}
public static void main(String[] args)
{
String arr[] = { "Welcome to geeks for geeks" ,
"Happy Coding" , "We love gfg" };
int ans = countMaxLength(arr);
System.out.print(ans);
}
}
|
Python3
def countMaxLength(arr, n):
max_len = 0
for i in range (n):
words = arr[i].split()
len_words = len (words)
if max_len < len_words:
max_len = len_words
return max_len
if __name__ = = "__main__" :
arr = [ "Welcome to geeks for geeks" ,
"Happy Coding" , "We love gfg" ]
n = len (arr)
ans = countMaxLength(arr, n)
print (ans)
|
C#
using System;
class Program
{
static int CountMaxLength( string [] arr, int n)
{
int max = 0;
for ( int i = 0; i < n; i++)
{
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++;
if (max < len)
max = len;
}
return max;
}
static void Main( string [] args)
{
string [] arr = { "Welcome to geeks for geeks" ,
"Happy Coding" , "We love gfg" };
int n = arr.Length;
int ans = CountMaxLength(arr, n);
Console.WriteLine(ans);
}
}
|
Javascript
function countMaxLength(arr) {
let max = 0;
for (let i = 0; i < arr.length; i++) {
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++;
if (max < len) {
max = len;
}
}
return max;
}
let arr = [ "Welcome to geeks for geeks" , "Happy Coding" , "We love gfg" ];
let ans = countMaxLength(arr);
console.log(ans);
|
Time Complexity: O(n*len)
Auxiliary Space: O(len)
Last Updated :
22 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...