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 = { “neveropen”, “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 { “neveropen”, “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
- Create a global counter array for each letter of size 26, initialize it with 0.
- For every lowercase alphabet check whether it exists in a current string
- If the alphabet exists in the string then increment the count.
- Repeat this for all strings of input.
C++
#include <iostream> #include <vector> #include <string> 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); // 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 = static_cast < char >(j + 'a' ); // checking whether the current string // contains the present Character if (str[i].find(c) != string::npos) { count[j]++; } } } // 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; } |
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 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 ]; // 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 |
Python3
# Python program to find count of Strings # for each letter [a-z] in the English alphabet # Function to find the countStrings # for each letter [a-z] def count_strings(strings): size = len (strings) # Initialize result as zero count = [ 0 ] * 26 # Loop through each string for i in range (size): # Loop through all the characters from 'a' to 'z' for j in range ( 26 ): c = chr (j + ord ( 'a' )) # Checking whether the current string # contains the present character if c in strings[i]: count[j] + = 1 # Print count for each letter for i in range ( 26 ): print (count[i], end = ' ' ) # Driver program if __name__ = = '__main__' : # Given list of strings strings = [ "i" , "will" , "practice" , "everyday" ] # Call the count_strings function count_strings(strings) |
C#
using System; using System.Collections.Generic; class Program { // Function to find the countStrings // for each letter [a-z] static void CountStrings(List< string > str) { int size = str.Count; // 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) { Console.Write(count[i] + " " ); } } // Driver program static void Main() { // Given array of Strings List< string > str = new List< string > { "i" , "will" , "practice" , "everyday" }; // Call the CountStrings function CountStrings(str); } } |
Javascript
// Function to find the countStrings // for each letter [a-z] function CountStrings(str) { const size = str.length; // Initialize result as zero const count = new Array(26).fill(0); // Loop through each Strings for (let i = 0; i < size; ++i) { // looping all the characters from a to z for (let j = 0; j < 26; j++) { const c = String.fromCharCode(j + 'a' .charCodeAt(0)); // checking whether the current string // contains the present Character if (str[i].includes(c)) { count[j]++; } } } // Print count for each letter for (let i = 0; i < 26; ++i) { process.stdout.write(count[i] + " " ); } } // Driver program function main() { // Given array of Strings const str = [ "i" , "will" , "practice" , "everyday" ]; // Call the CountStrings function CountStrings(str); } main(); |
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 <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; } |
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 |
Javascript
<script> // JavaScript program to find count of strings // for each letter [a-z] in english alphabet // Function to find the countStrings // for each letter [a-z] function CountStrings(str) { let size = str.length; // Initialize result as zero let count = new Array(26).fill(0); // Mark all letter as not visited let visited = new Array(26).fill( false ); // Loop through each strings for (let i = 0; i < size; ++i) { for (let j = 0; j < str[i].length; ++j) { // Increment the global counter // for current character of string if (visited[str[i].charCodeAt(j) - 97] == false ) count[str[i].charCodeAt(j) - 97]++; visited[str[i].charCodeAt(j) - 97] = true ; } // Instead of re-initialising boolean // vector every time we just reset // all visited letter to false for (let j = 0; j < str[i].length; ++j) { visited[str[i].charCodeAt(j) - 97] = false ; } } // Print count for each letter for (let i = 0; i < 26; ++i) { document.write(count[i], " " ); } } // Driver program // Given array of strings let str = [ "i" , "will" , "practice" , "everyday" ]; // Call the countStrings function CountStrings(str); // This code is contributed by shinjanpatra </script> |
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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!