Count maximum number of words in Array
Last Updated :
22 Mar, 2023
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;
// 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
/* 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
# 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
C#
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
// 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);
Time Complexity: O(n*len)
Auxiliary Space: O(len)
Similar Reads
Counting the number of words in a Trie A Trie is used to store dictionary words so that they can be searched efficiently and prefix search can be done. The task is to write a function to count the number of words. Example : Input : root / \ \ t a b | | | h n y | | \ | e s y e / | | i r w | | | r e e | r Output : 8 Explanation : Words for
7 min read
Count of number of given string in 2D character array Given a 2-dimensional character array and a string, we need to find the given string in a 2-dimensional character array, such that individual characters can be present left to right, right to left, top to down or down to top. Examples: Input : a ={ {D,D,D,G,D,D}, {B,B,D,E,B,S}, {B,S,K,E,B,K}, {D,D,D
9 min read
Find all concatenations of words in Array Given an array of strings arr[] (1 <= |arr[i]| <= 20) of size N (1 <= N <= 104), the task is to find all strings from the given array that are formed by the concatenation of strings in the same given array. Examples: Input: arr[]= { "geek", "geeks", "for", "geeksforgeeks", "g", "f", "g",
9 min read
Lex Program to count number of words Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. Prerequisite: Flex (Fast lexical Analyzer Generator) Examp
1 min read
Count palindrome words in a sentence Given a string str and the task is to count palindrome words present in the string str. Examples: Input : Madam Arora teaches malayalam Output : 3 The string contains three palindrome words (i.e., Madam, Arora, malayalam) so the count is three. Input : Nitin speaks malayalam Output : 2 The string co
5 min read