Acronym words
Last Updated :
15 Sep, 2022
Given N strings consisting of lowercase letters of the English alphabet. The task is to find how many strings out of these N strings are an acronym for other N – 1 strings.
For a subset of strings, we can choose to order them in any way and then concatenate the first letter of each of them. For example, csa is an acronym for the subset {computer, academy, science} ans so is acs. Print the number of strings that can be an acronym of other strings.
Examples:
Input: arr[] = {“abc”, “bcad”, “cabd”, “cba”, “dzzz”}
Output: 2
cabd is an acronym for {cba, abc, bcad, dzzz}
cba is an acronym for {cabd, bcad, abc}
Input: arr[] = {“gnu”, “not”, “unix”}
Output: 0
Note that gnu is not an acronym for {gnu, not, unix}
Approach: Suppose we have a frequency array freq where freq[i] is the number of times character i is the first in the given strings. In order to check if a string S can be an acronym, first, we should decrease the frequency of the first letter of S then check if the frequency of every letter in S is less than or equal to its value in freq[] array.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int count_acronym( int n, string arr[])
{
int freq[26] = {0};
for ( int i = 0; i < n; i++)
freq[arr[i][0] - 'a' ]++;
int cnt = 0;
for ( int i = 0; i < n; i++)
{
string st = arr[i];
int num[26] = {0};
for ( int j = 0; j < st.length(); j++)
num[st[j] - 'a' ]++;
bool flag = true ;
for ( int j = 1; j < 26; j++)
{
if (num[j] > freq[j])
{
flag = false ;
break ;
}
}
int x = st[0] - 'a' ;
if (freq[x] - 1 < num[x])
flag = false ;
if (flag)
cnt++;
}
return cnt;
}
int main()
{
string arr[] = { "abc" , "bcad" , "cabd" ,
"cba" , "dzzz" };
int n = 5;
cout << count_acronym(n, arr);
}
|
Java
class GFG {
static int count_acronym( int n, String[] arr)
{
int [] freq = new int [ 26 ];
for ( int i = 0 ; i < n; i++)
freq[arr[i].charAt( 0 ) - 'a' ]++;
int cnt = 0 ;
for ( int i = 0 ; i < n; i++) {
String st = arr[i];
int [] num = new int [ 26 ];
for ( int j = 0 ; j < st.length(); j++)
num[st.charAt(j) - 'a' ]++;
boolean flag = true ;
for ( int j = 1 ; j < 26 ; j++) {
if (num[j] > freq[j]) {
flag = false ;
break ;
}
}
int x = st.charAt( 0 ) - 'a' ;
if (freq[x] - 1 < num[x])
flag = false ;
if (flag)
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
String[] arr = { "abc" ,
"bcad" ,
"cabd" ,
"cba" ,
"dzzz" };
int n = arr.length;
System.out.println(count_acronym(n, arr));
}
}
|
Python3
def count_acronym(n, arr):
freq = [ 0 ] * 26
for i in range (n):
freq[ ord (arr[i][ 0 ]) - ord ( 'a' )] + = 1
cnt = 0
for i in range (n):
st = arr[i]
num = [ 0 ] * 26
for j in range ( len (st)):
num[ ord (st[j]) - ord ( 'a' )] + = 1
flag = True
for j in range ( 1 , 26 ):
if num[j] > freq[j]:
flag = False
break
x = ord (st[ 0 ]) - ord ( 'a' )
if freq[x] - 1 < num[x]:
flag = False
if flag:
cnt + = 1
return cnt
if __name__ = = "__main__" :
arr = [ "abc" , "bcad" , "cabd" , "cba" , "dzzz" ]
n = 5
print (count_acronym(n, arr))
|
C#
using System;
class GFG
{
static int count_acronym( int n, string [] arr)
{
int [] freq = new int [26];
for ( int i = 0; i < n; i++)
freq[arr[i][0] - 'a' ]++;
int cnt = 0;
for ( int i = 0; i < n; i++)
{
string st = arr[i];
int [] num = new int [26];
for ( int j = 0; j < st.Length; j++)
num[st[j] - 'a' ]++;
bool flag = true ;
for ( int j = 1; j < 26; j++)
{
if (num[j] > freq[j])
{
flag = false ;
break ;
}
}
int x = st[0] - 'a' ;
if (freq[x] - 1 < num[x])
flag = false ;
if (flag)
cnt++;
}
return cnt;
}
public static void Main()
{
string [] arr = { "abc" ,
"bcad" ,
"cabd" ,
"cba" ,
"dzzz" };
int n = arr.Length;
Console.WriteLine(count_acronym(n, arr));
}
}
|
Javascript
<script>
function count_acronym(n, arr)
{
let freq = new Array(26);
freq.fill(0);
for (let i = 0; i < n; i++)
freq[arr[i][0].charCodeAt() - 'a' .charCodeAt()]++;
let cnt = 0;
for (let i = 0; i < n; i++)
{
let st = arr[i];
let num = new Array(26);
num.fill(0);
for (let j = 0; j < st.length; j++)
num[st[j].charCodeAt() - 'a' .charCodeAt()]++;
let flag = true ;
for (let j = 1; j < 26; j++)
{
if (num[j] > freq[j])
{
flag = false ;
break ;
}
}
let x = st[0].charCodeAt() - 'a' .charCodeAt();
if (freq[x] - 1 < num[x])
flag = false ;
if (flag)
cnt++;
}
return cnt;
}
let arr = [ "abc" ,
"bcad" ,
"cabd" ,
"cba" ,
"dzzz" ];
let n = arr.length;
document.write(count_acronym(n, arr));
</script>
|
Share your thoughts in the comments
Please Login to comment...