📜  所有给定句子中的最小单词数

📅  最后修改于: 2022-05-13 01:57:08.817000             🧑  作者: Mango

所有给定句子中的最小单词数

给定N个小写句子,任务是在所有这些句子中找到最少的单词数。

例子:

方法:这是一个基于实现的问题。这个想法是找到一个句子中所有给定句子的单词计数,并在一个变量中保持最小计数,这是所需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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



输出
4

时间复杂度: O(N * M) 其中 M 是所有句子的平均字符数
辅助空间: O(1)