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<bits/stdc++.h> 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 |
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)); } } |
Python3
# Python3 implementation of the approach # Function to return the number of strings # that can be an acronym for other strings def count_acronym(n, arr): # Frequency array to store the # frequency of the first character # of every string in the array freq = [0] * 26 for i in range(n): freq[ord(arr[i][0]) - ord('a')] += 1 # To store the count of required strings cnt = 0 for i in range(n): # Current word st = arr[i] # Frequency array to store the # frequency of each of the character # of the current string num = [0] * 26 for j in range(len(st)): num[ord(st[j]) - ord('a')] += 1 flag = True # Check if the frequency of every character in # the current string is <= its value in freq[] for j in range(1, 26): if num[j] > freq[j]: flag = False break # First character of the current string x = ord(st[0]) - ord('a') if freq[x] - 1 < num[x]: flag = False if flag: cnt += 1 return cnt # Driver Code if __name__ == "__main__": arr = ["abc", "bcad", "cabd", "cba", "dzzz"] n = 5 print(count_acronym(n, arr)) # This code is contributed by # sanjeev2552 |
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
- Reverse individual words
- Possible Words using given characters in Python
- Split a sentence into words in C++
- Count words in a given string
- Reverse words in a given string
- Group words with same set of characters
- Count of words that are present in all the given sentences
- Reverse String according to the number of words
- Count palindrome words in a sentence
- Find words which are greater than given length k
- Print all funny words in a string
- Number of words in a camelcase sequence
- Minimum Distance Between Words of a String
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.



