For each lowercase English alphabet find the count of strings having these alphabets
Last Updated :
16 Oct, 2023
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
- 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;
void CountStrings(vector<string>& str)
{
int size = str.size();
vector< int > count(26, 0);
for ( int i = 0; i < size; ++i) {
for ( int j = 0; j < 26; j++) {
char c = static_cast < char >(j + 'a' );
if (str[i].find(c) != string::npos) {
count[j]++;
}
}
}
for ( int i = 0; i < 26; ++i) {
cout << count[i] << " " ;
}
}
int main()
{
vector<string> str
= { "i" , "will" , "practice" , "everyday" };
CountStrings(str);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void CountStrings(String[] str)
{
int size = str.length;
int [] count = new int [ 26 ];
for ( int i = 0 ; i < size; ++i) {
for ( int j = 0 ; j < 26 ; j++) {
char c = ( char )(j + 'a' );
if (str[i].contains( "" + c)) {
count[j]++;
}
}
}
for ( int i = 0 ; i < 26 ; ++i) {
System.out.print(count[i] + " " );
}
}
public static void main(String[] args)
{
String[] str
= { "i" , "will" , "practice" , "everyday" };
CountStrings(str);
}
}
|
Python3
def count_strings(strings):
size = len (strings)
count = [ 0 ] * 26
for i in range (size):
for j in range ( 26 ):
c = chr (j + ord ( 'a' ))
if c in strings[i]:
count[j] + = 1
for i in range ( 26 ):
print (count[i], end = ' ' )
if __name__ = = '__main__' :
strings = [ "i" , "will" , "practice" , "everyday" ]
count_strings(strings)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void CountStrings(List< string > str)
{
int size = str.Count;
int [] count = new int [26];
for ( int i = 0; i < size; ++i)
{
for ( int j = 0; j < 26; j++)
{
char c = ( char )(j + 'a' );
if (str[i].Contains(c))
{
count[j]++;
}
}
}
for ( int i = 0; i < 26; ++i)
{
Console.Write(count[i] + " " );
}
}
static void Main()
{
List< string > str = new List< string > { "i" , "will" , "practice" , "everyday" };
CountStrings(str);
}
}
|
Javascript
function CountStrings(str) {
const size = str.length;
const count = new Array(26).fill(0);
for (let i = 0; i < size; ++i) {
for (let j = 0; j < 26; j++) {
const c = String.fromCharCode(j + 'a' .charCodeAt(0));
if (str[i].includes(c)) {
count[j]++;
}
}
}
for (let i = 0; i < 26; ++i) {
process.stdout.write(count[i] + " " );
}
}
function main() {
const str = [ "i" , "will" , "practice" , "everyday" ];
CountStrings(str);
}
main();
|
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++
#include <bits/stdc++.h>
using namespace std;
void CountStrings(vector<string>& str)
{
int size = str.size();
vector< int > count(26, 0);
vector< bool > visited(26, false );
for ( int i = 0; i < size; ++i)
{
for ( int j = 0; j < str[i].length(); ++j)
{
if (visited[str[i][j]] == false )
count[str[i][j] - 'a' ]++;
visited[str[i][j]] = true ;
}
for ( int j = 0; j < str[i].length(); ++j)
{
visited[str[i][j]] = false ;
}
}
for ( int i = 0; i < 26; ++i)
{
cout << count[i] << " " ;
}
}
int main()
{
vector<string> str = { "i" , "will" ,
"practice" , "everyday" };
CountStrings(str);
return 0;
}
|
Java
class GFG{
static void CountStrings(String []str)
{
int size = str.length;
int []count = new int [ 26 ];
boolean []visited = new boolean [ 26 ];
for ( int i = 0 ; i < size; ++i)
{
for ( int j = 0 ; j < str[i].length(); ++j)
{
if (visited[str[i].charAt(j) - 'a' ] == false )
count[str[i].charAt(j) - 'a' ]++;
visited[str[i].charAt(j) - 'a' ] = true ;
}
for ( int j = 0 ; j < str[i].length(); ++j)
{
visited[str[i].charAt(j) - 'a' ] = false ;
}
}
for ( int i = 0 ; i < 26 ; ++i)
{
System.out.print(count[i] + " " );
}
}
public static void main(String[] args)
{
String []str = { "i" , "will" ,
"practice" , "everyday" };
CountStrings(str);
}
}
|
Python3
def CountStrings(s):
size = len (s)
count = [ 0 ] * 26
visited = [ False ] * 26
for i in range (size):
for j in range ( len (s[i])):
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
for j in range ( len (s[i])):
visited[ ord (s[i][j]) -
ord ( 'a' )] = False
for i in range ( 26 ):
print (count[i], end = ' ' )
if __name__ = = '__main__' :
s = [ "i" , "will" ,
"practice" , "everyday" ]
CountStrings(s)
|
C#
using System;
class GFG{
static void CountStrings(String []str)
{
int size = str.Length;
int []count = new int [26];
bool []visited = new bool [26];
for ( int i = 0; i < size; ++i)
{
for ( int j = 0; j < str[i].Length; ++j)
{
if (visited[str[i][j] - 'a' ] == false )
count[str[i][j] - 'a' ]++;
visited[str[i][j] - 'a' ] = true ;
}
for ( int j = 0; j < str[i].Length; ++j)
{
visited[str[i][j] - 'a' ] = false ;
}
}
for ( int i = 0; i < 26; ++i)
{
Console.Write(count[i] + " " );
}
}
public static void Main(String[] args)
{
String []str = { "i" , "will" ,
"practice" , "everyday" };
CountStrings(str);
}
}
|
Javascript
<script>
function CountStrings(str)
{
let size = str.length;
let count = new Array(26).fill(0);
let visited = new Array(26).fill( false );
for (let i = 0; i < size; ++i)
{
for (let j = 0; j < str[i].length; ++j)
{
if (visited[str[i].charCodeAt(j) - 97] == false )
count[str[i].charCodeAt(j) - 97]++;
visited[str[i].charCodeAt(j) - 97] = true ;
}
for (let j = 0; j < str[i].length; ++j)
{
visited[str[i].charCodeAt(j) - 97] = false ;
}
}
for (let i = 0; i < 26; ++i)
{
document.write(count[i], " " );
}
}
let str = [ "i" , "will" , "practice" , "everyday" ];
CountStrings(str);
</script>
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...