Acronym words
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++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the number of strings
// that can be an acronym for other strings
int count_acronym(int n, string arr[])
{
// Frequency array to store the
// frequency of the first character
// of every string in the array
int freq[26] = {0};
for (int i = 0; i < n; i++)
freq[arr[i][0] - 'a']++;
// To store the count of
// required strings
int cnt = 0;
for (int i = 0; i < n; i++)
{
// Current word
string st = arr[i];
// Frequency array to store the
// frequency of each of the character
// of the current string
int num[26] = {0};
for (int j = 0; j < st.length(); j++)
num[st[j] - 'a']++;
bool flag = true;
// Check if the frequency of every character in
// the current string is <= its value in freq[]
for (int j = 1; j < 26; j++)
{
if (num[j] > freq[j])
{
flag = false;
break;
}
}
// First character of the current string
int x = st[0] – ‘a’;
if (freq[x] – 1 < num[x])
flag = false;
if (flag)
cnt++;
}
return cnt;
}
// Driver code
int main()
{
string arr[] = {"abc", "bcad", "cabd",
"cba", "dzzz"};
int n = 5;
cout << count_acronym(n, arr);
}
// This code is contributed by
// Surendra_Gangwar
[tabby title = "Java"]
// Java implementation of the approach class GFG { // Function to return the number of strings // that can be an acronym for other strings static int count_acronym(int n, String[] arr) { // Frequency array to store the frequency // of the first character of // every string in the array int[] freq = new int[26]; for (int i = 0; i < n; i++) freq[arr[i].charAt(0) - 'a']++; // To store the count of required strings int cnt = 0; for (int i = 0; i < n; i++) { // Current word String st = arr[i]; // Frequency array to store the frequency // of each of the character // of the current string int[] num = new int[26]; for (int j = 0; j < st.length(); j++) num[st.charAt(j) - 'a']++; boolean flag = true; // Check if the frequency of every character in // the current string is <= its value in freq[] for (int j = 1; j < 26; j++) { if (num[j] > freq[j]) { flag = false; break; } } // First character of the current string int x = st.charAt(0) - 'a'; if (freq[x] - 1 < num[x]) flag = false; if (flag) cnt++; } return cnt; } // Driver code public static void main(String[] args) { String[] arr = { "abc", "bcad", "cabd", "cba", "dzzz" }; int n = arr.length; System.out.println(count_acronym(n, arr)); } } |
C#
// C# implementation of the approach using System; class GFG { // Function to return the number of strings // that can be an acronym for other strings static int count_acronym(int n, string[] arr) { // Frequency array to store the frequency // of the first character of // every string in the array int[] freq = new int[26]; for (int i = 0; i < n; i++) freq[arr[i][0] - 'a']++; // To store the count of required strings int cnt = 0; for (int i = 0; i < n; i++) { // Current word string st = arr[i]; // Frequency array to store the frequency // of each of the character // of the current string int[] num = new int[26]; for (int j = 0; j < st.Length; j++) num[st[j] - 'a']++; bool flag = true; // Check if the frequency of every character in // the current string is <= its value in freq[] for (int j = 1; j < 26; j++) { if (num[j] > freq[j]) { flag = false; break; } } // First character of the current string int x = st[0] - 'a'; if (freq[x] - 1 < num[x]) flag = false; if (flag) cnt++; } return cnt; } // Driver code public static void Main() { string[] arr = { "abc", "bcad", "cabd", "cba", "dzzz" }; int n = arr.Length; Console.WriteLine(count_acronym(n, arr)); } } // This code is contributed by Ryuga |
2
Recommended Posts:
- Check if the given string of words can be formed from words present in the dictionary
- Count words that appear exactly two times in an array of words
- Possible Words using given characters in Python
- Split a sentence into words in C++
- Reverse words in a given string
- Reverse individual words
- Group words with same set of characters
- Count words in a given string
- Find words which are greater than given length k
- Print all funny words in a string
- Count palindrome words in a sentence
- Minimum Distance Between Words of a String
- Reverse words in a given String in Java
- Reverse middle words of a string
- Generating Lyndon words of length n
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.



