Minimum count of words among all the given sentences
Last Updated :
11 Jan, 2022
Given N lowercase sentences, the task is to find the minimum count of words among all of these sentences.
Examples:
Input: arr[] = {
“there is a cow”,
“cow is our mother”,
“cow gives us milk and milk is sweet”,
“there is a boy who loves cow”}
Output: 4
Explanation: Both 1st and the second sentence has 4 words which is the minimum possible.
Input: arr[] = {
“abc aac abcd ccc”,
“ac abc cca”,
“abca aaac abcd ccc”}
Output: 3
Approach: This is an implementation-based problem. The idea is to find the count of words in a sentence for all the given sentences and maintain the minimum count in a variable which is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countword(string& a)
{
int count = 1;
for ( int i = 0; i < a.size(); i++) {
if (a[i] == ' ' )
count++;
}
return count;
}
int countleastWords(string arr[], int N)
{
int ans = INT_MAX;
for ( int i = 0; i < N; i++) {
int temp = countword(arr[i]);
if (temp < ans)
ans = temp;
}
return ans;
}
int main()
{
string arr[] = { "there is a cow" ,
"cow is our mother" ,
"cow gives us milk and \
milk is sweet",
"there is a boy who loves cow" };
int N = 4;
cout << countleastWords(arr, N) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int countword(String a)
{
int count = 1 ;
for ( int i = 0 ; i < a.length(); i++) {
if (a.charAt(i) == ' ' )
count++;
}
return count;
}
static int countleastWords(String[] arr, int N)
{
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++) {
int temp = countword(arr[i]);
if (temp < ans)
ans = temp;
}
return ans;
}
public static void main(String args[])
{
String[] arr
= { "there is a cow" , "cow is our mother" ,
"cow gives us milk and milk is sweet" ,
"there is a boy who loves cow" };
int N = 4 ;
System.out.print(countleastWords(arr, N));
}
}
|
Python3
def countword(a) :
count = 1 ;
for i in range ( len (a)):
if (a[i] = = ' ' ):
count + = 1
return count;
def countleastWords(arr, N):
ans = 10 * * 9 ;
for i in range (N):
temp = countword(arr[i]);
if (temp < ans):
ans = temp;
return ans;
arr = [ "there is a cow" ,
"cow is our mother" ,
"cow gives us milk and \
milk is sweet",
"there is a boy who loves cow" ];
N = 4 ;
print (countleastWords(arr, N));
|
C#
using System;
class GFG
{
static int countword( string a)
{
int count = 1;
for ( int i = 0; i < a.Length; i++) {
if (a[i] == ' ' )
count++;
}
return count;
}
static int countleastWords( string [] arr, int N)
{
int ans = Int32.MaxValue;
for ( int i = 0; i < N; i++) {
int temp = countword(arr[i]);
if (temp < ans)
ans = temp;
}
return ans;
}
public static void Main()
{
string [] arr
= { "there is a cow" , "cow is our mother" ,
"cow gives us milk and milk is sweet" ,
"there is a boy who loves cow" };
int N = 4;
Console.Write(countleastWords(arr, N));
}
}
|
Javascript
<script>
function countword(a)
{
let count = 1;
for (let i = 0; i < a.length; i++) {
if (a[i] == ' ' )
count++;
}
return count;
}
function countleastWords(arr, N)
{
let ans = Number.MAX_VALUE;
for (let i = 0; i < N; i++) {
let temp = countword(arr[i]);
if (temp < ans)
ans = temp;
}
return ans;
}
let arr = [ "there is a cow" ,
"cow is our mother" ,
"cow gives us milk and \
milk is sweet" ,
"there is a boy who loves cow" ];
let N = 4;
document.write(countleastWords(arr, N) + "<br>" );
</script>
|
Time Complexity: O(N * M) where M is the average number of characters for all sentences
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...