Minimum count of words among all the given sentences
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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find count of // words in the given string int countword(string& a) { // Stores word count int count = 1; for ( int i = 0; i < a.size(); i++) { // If a new word is // encountered if (a[i] == ' ' ) count++; } // Return count return count; } // Function to find minimum count of // words among all given sentences int countleastWords(string arr[], int N) { // Stores the final answer int ans = INT_MAX; // Loop to iterate over sentences for ( int i = 0; i < N; i++) { // Stores count of words // in ith sentence int temp = countword(arr[i]); // Update answer if (temp < ans) ans = temp; } // Return Answer return ans; } // Driver code 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
// Java code to implement above approach import java.util.*; public class GFG { // Function to find count of // words in the given string static int countword(String a) { // Stores word count int count = 1 ; for ( int i = 0 ; i < a.length(); i++) { // If a new word is // encountered if (a.charAt(i) == ' ' ) count++; } // Return count return count; } // Function to find minimum count of // words among all given sentences static int countleastWords(String[] arr, int N) { // Stores the final answer int ans = Integer.MAX_VALUE; // Loop to iterate over sentences for ( int i = 0 ; i < N; i++) { // Stores count of words // in ith sentence int temp = countword(arr[i]); // Update answer if (temp < ans) ans = temp; } // Return Answer return ans; } // Driver code 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)); } } // This code is contributed by Samim Hossain Mondal. |
Python3
# Python code for the above approach # Function to find count of # words in the given string def countword(a) : # Stores word count count = 1 ; for i in range ( len (a)): # If a new word is # encountered if (a[i] = = ' ' ): count + = 1 # Return count return count; # Function to find minimum count of # words among all given sentences def countleastWords(arr, N): # Stores the final answer ans = 10 * * 9 ; # Loop to iterate over sentences for i in range (N): # Stores count of words # in ith sentence temp = countword(arr[i]); # Update answer if (temp < ans): ans = temp; # Return Answer return ans; # Driver code 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)); # This code is contributed by gfgking |
C#
// C# code to implement above approach using System; class GFG { // Function to find count of // words in the given string static int countword( string a) { // Stores word count int count = 1; for ( int i = 0; i < a.Length; i++) { // If a new word is // encountered if (a[i] == ' ' ) count++; } // Return count return count; } // Function to find minimum count of // words among all given sentences static int countleastWords( string [] arr, int N) { // Stores the final answer int ans = Int32.MaxValue; // Loop to iterate over sentences for ( int i = 0; i < N; i++) { // Stores count of words // in ith sentence int temp = countword(arr[i]); // Update answer if (temp < ans) ans = temp; } // Return Answer return ans; } // Driver code 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)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach // Function to find count of // words in the given string function countword(a) { // Stores word count let count = 1; for (let i = 0; i < a.length; i++) { // If a new word is // encountered if (a[i] == ' ' ) count++; } // Return count return count; } // Function to find minimum count of // words among all given sentences function countleastWords(arr, N) { // Stores the final answer let ans = Number.MAX_VALUE; // Loop to iterate over sentences for (let i = 0; i < N; i++) { // Stores count of words // in ith sentence let temp = countword(arr[i]); // Update answer if (temp < ans) ans = temp; } // Return Answer return ans; } // Driver code 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>" ); // This code is contributed by Potta Lokesh </script> |
4
Time Complexity: O(N * M) where M is the average number of characters for all sentences
Auxiliary Space: O(1)
Please Login to comment...