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] 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.

Naive Approach:



  1. Create a global counter for each letter of size 26, initialize it with 0.
  2. Run a loop for each of small letter English alphabet. Increment the counter of, say letter ‘x’, if the current letter is found in the current string.
  3. Repeat this for all lowercase English alphabet.

Time Complexity: O(26*N), where N is the sum of the length of all 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find count of strings
// for each letter [a-z] in english alphabet
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the countStrings
// for each letter [a-z]
void CountStrings(vector<string>& str)
{
    int size = str.size();
  
    // Initialize result as zero
    vector<int> count(26, 0);
  
    // Mark all letter as not visited
    vector<bool> 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<string> str = {"i", "will",
                          "practice", "everyday"};
  
    // Call the countStrings function
    CountStrings(str);
  
    return 0;
}

chevron_right


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 length of all strings.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.