GeeksforGeeks App
Open App
Browser
Continue

# For each lowercase English alphabet find the count of strings having these alphabets

Given an array of strings of lowercase English alphabets. The task is for each letter [a-z] to find the count of strings having these letters.
Examples:

Input: str = { “geeks”, “for”, “code” }
Output: { 0 0 1 1 2 1 1 0 0 0 0 0 0 0 2 0 0 1 1 0 0 0 0 0 0 0 }
Explanation:
For a letter, say ‘e’, it is present in { “geeks”, “code” }, hence its count is 2.
Similarly for another letter result can be found.

Input: str = { “i”, “will”, “practice”, “everyday” }
Output: 2 0 1 1 2 0 0 0 3 0 0 0 0 0 0 1 0 2 0 1 0 1 1 0 1 0
Explanation:
For a letter, say ‘i’, it is present in { “i”, “will”, “practice” }, hence its count is 3.
Similarly for another letter result can be found.

### Method : Brute Force

1. Create a global counter array for each letter of size 26, initialize it with 0.
2. For every lowercase alphabet check whether it exists in a current string
3. If the alphabet exists in the string then increment the count.
4. Repeat this for all strings of input.

## Java

 /*package whatever //do not write package name here */ import java.io.*; // Java program to find count of Strings// for each letter [a-z] in english alphabetclass 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];         // Loop through each Strings        for (int i = 0; i < size; ++i) {            // looping all the characters from a to z            for (int j = 0; j < 26; j++) {                char c = (char)(j + 'a');                 // checking whether the current string                // contains the present Character                if (str[i].contains("" + c)) {                    count[j]++;                }                           }        }         // 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 aeroabrar_31

Output

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

Time Complexity: O(N*26*(len))

where N is length of input array and len is maximum length of strings.

Efficient Approach:

• Instead of running a loop for each small letter English alphabet and checking whether it is present in the current string or not. We can instead run a loop on each string individually and increment the global counter for any letter present in that string.
• Also, to avoid duplicate count we create a visited boolean array to mark the characters encounter so far. This approach reduces the complexity to O(N).

Below is the implementation of the above approach:

## 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 programint 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 alphabetclass 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 programpublic 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 codeif __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 alphabetusing 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 codepublic 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

## Javascript



Output:

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

Time Complexity: O(N), where N is the sum of the length of all strings.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up