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:
- Create a global counter for each letter of size 26, initialize it with 0.
- 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.
- 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:
C++
// 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; } |
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 |
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count characters at same position as in English alphabet
- Count of character pairs at same distance as in English alphabets
- Check if frequency of each character is equal to its position in English Alphabet
- Find the Mid-Alphabet for each index of the given Pair of Strings
- Maximum distinct lowercase alphabets between two uppercase
- Sub-string that contains all lowercase alphabets after performing the given operation
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Check if count of Alphabets and count of Numbers are equal in the given String
- Count Uppercase, Lowercase, special character and numeric values
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Count of alphabets having ASCII value less than and greater than k
- Find the most valued alphabet in the String
- Count and Print the alphabets having ASCII value in the range [l, r]
- Count and Print the alphabets having ASCII value not in the range [l, r]
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find letter's position in Alphabet using Bit operation
- Count of alphabets whose ASCII values can be formed with the digits of N
- Find the length of the longest subsequence with first K alphabets having same frequency
- Count of same length Strings that exists lexicographically in between two given Strings
- Count of strings that become equal to one of the two strings after one removal
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.
Improved By : rutvik_56

