📌  相关文章
📜  对于每个小写英文字母,查找具有这些字母的字符串的数量

📅  最后修改于: 2021-05-14 01:55:34             🧑  作者: Mango

给定一个由小写英文字母组成的字符串数组。任务是为每个字母[az]查找具有这些字母的字符串的数量。
例子:

天真的方法:

  1. 为每个大小为26的字母创建一个全局计数器,并将其初始化为0。
  2. 为每个小字母的英文字母运行一个循环。如果在当前字符串找到当前字母,则增加字母’x’的计数器。
  3. 对所有小写英文字母重复此操作。

时间复杂度: O(26 * N),其中N是所有字符串的长度之和。

高效方法:

  • 而不是为每个小字母英文字母运行循环并检查它是否存在于当前字符串。相反,我们可以在每个字符串单独运行一个循环,并为该字符串存在的任何字母增加全局计数器。
  • 另外,为了避免重复计数,我们创建了一个访问过的布尔数组来标记到目前为止遇到的字符。这种方法将复杂度降低到O(N)。

下面是上述方法的实现:

C++
// C++ program to find count of strings
// for each letter [a-z] in english alphabet
#include 
using namespace std;
  
// Function to find the countStrings
// for each letter [a-z]
void CountStrings(vector& str)
{
    int size = str.size();
  
    // Initialize result as zero
    vector count(26, 0);
  
    // Mark all letter as not visited
    vector visited(26, false);
  
    // Loop through each strings
    for (int i = 0; i < size; ++i)
    {
  
        for (int j = 0; j < str[i].length(); ++j)
        {
  
            // Increment the global counter
            // for current character of string
            if (visited[str[i][j]] == false)
                count[str[i][j] - 'a']++;
  
            visited[str[i][j]] = true;
        }
  
        // Instead of re-initialising boolean
        // vector every time we just reset
        // all visited letter to false
        for (int j = 0; j < str[i].length(); ++j)
        {
            visited[str[i][j]] = false;
        }
    }
  
    // Print count for each letter
    for (int i = 0; i < 26; ++i)
    {
        cout << count[i] << " ";
    }
}
  
// Driver program
int main()
{
    // Given array of strings
    vector str = {"i", "will",
                          "practice", "everyday"};
  
    // Call the countStrings function
    CountStrings(str);
  
    return 0;
}


Java
// Java program to find count of Strings
// for each letter [a-z] in english alphabet
class GFG{
  
// Function to find the countStrings
// for each letter [a-z]
static void CountStrings(String []str)
{
    int size = str.length;
  
    // Initialize result as zero
    int []count = new int[26];
  
    // Mark all letter as not visited
    boolean []visited = new boolean[26];
    // Loop through each Strings
    for (int i = 0; i < size; ++i)
    {
        for (int j = 0; j < str[i].length(); ++j)
        {
            // Increment the global counter
            // for current character of String
            if (visited[str[i].charAt(j) - 'a'] == false)
                count[str[i].charAt(j) - 'a']++;
  
            visited[str[i].charAt(j) - 'a'] = true;
        }
  
        // Instead of re-initialising boolean
        // vector every time we just reset
        // all visited letter to false
        for (int j = 0; j < str[i].length(); ++j)
        {
            visited[str[i].charAt(j) - 'a'] = false;
        }
    }
  
    // Print count for each letter
    for (int i = 0; i < 26; ++i)
    {
        System.out.print(count[i] + " ");
    }
}
  
// Driver program
public static void main(String[] args)
{
    // Given array of Strings
    String []str = {"i", "will",
                    "practice", "everyday"};
  
    // Call the countStrings function
    CountStrings(str);
}
}
  
// This code is contributed by shikhasingrajput


Python3
# Python3 program to find count of 
# strings for each letter [a-z] in 
# english alphabet
  
# Function to find the countStrings 
# for each letter [a-z] 
def CountStrings(s):
      
    size = len(s)
      
    # Initialize result as zero
    count = [0] * 26
      
    # Mark all letter as not visited
    visited = [False] * 26
      
    # Loop through each strings
    for i in range(size):
        for j in range(len(s[i])):
              
            # Increment the global counter
            # for current character of string
            if visited[ord(s[i][j]) - 
                       ord('a')] == False:
                count[ord(s[i][j]) - 
                      ord('a')] += 1
                  
            visited[ord(s[i][j]) - 
                    ord('a')] = True
              
        # Instead of re-initialising boolean
        # vector every time we just reset
        # all visited letter to false
        for j in range(len(s[i])):
            visited[ord(s[i][j]) - 
                    ord('a')] = False
              
    # Print count for each letter
    for i in range(26):
        print(count[i], end = ' ')
  
# Driver code
if __name__=='__main__':
      
    # Given array of strings
    s = [ "i", "will", 
          "practice", "everyday" ]
  
    # Call the countStrings function
    CountStrings(s)
  
# This code is contributed by rutvik_56


C#
// C# program to find count of Strings
// for each letter [a-z] in english alphabet
using System;
  
class GFG{
  
// Function to find the countStrings
// for each letter [a-z]
static void CountStrings(String []str)
{
    int size = str.Length;
  
    // Initialize result as zero
    int []count = new int[26];
  
    // Mark all letter as not visited
    bool []visited = new bool[26];
      
    // Loop through each Strings
    for(int i = 0; i < size; ++i)
    {
        for(int j = 0; j < str[i].Length; ++j)
        {
              
            // Increment the global counter
            // for current character of String
            if (visited[str[i][j] - 'a'] == false)
                count[str[i][j] - 'a']++;
  
            visited[str[i][j] - 'a'] = true;
        }
  
        // Instead of re-initialising bool
        // vector every time we just reset
        // all visited letter to false
        for(int j = 0; j < str[i].Length; ++j)
        {
            visited[str[i][j] - 'a'] = false;
        }
    }
  
    // Print count for each letter
    for(int i = 0; i < 26; ++i)
    {
        Console.Write(count[i] + " ");
    }
}
  
// Driver code
public static void Main(String[] args)
{
      
    // Given array of Strings
    String []str = { "i", "will",
                     "practice", "everyday"};
  
    // Call the countStrings function
    CountStrings(str);
}
}
  
// This code is contributed by Amit Katiyar


输出:
2 0 1 1 2 0 0 0 3 0 0 1 0 0 0 1 0 2 0 1 0 1 1 0 1 0

时间复杂度: O(N),其中N是所有字符串的长度之和。