Acronym words - GeeksforGeeks

02 Jun, 2021
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++
Java
Python3
C#
Javascript
// 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 stringsint 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 codeint main(){    string arr[] = {"abc", "bcad", "cabd",                    "cba", "dzzz"};    int n = 5;    cout << count_acronym(n, arr);} // This code is contributed by// Surendra_Gangwar
// Java implementation of the approachclass 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 implementation of the approach # Function to return the number of strings# that can be an acronym for other stringsdef 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 Codeif __name__ == "__main__":    arr = ["abc", "bcad", "cabd", "cba", "dzzz"]    n = 5    print(count_acronym(n, arr)) # This code is contributed by# sanjeev2552
// C# implementation of the approachusing 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
<script>     // JavaScript implementation of the approach         // Function to return the number of strings    // that can be an acronym for other strings    function count_acronym(n, arr)    {        // Frequency array to store the frequency        // of the first character of        // every string in the array        let freq = new Array(26);        freq.fill(0);           for (let i = 0; i < n; i++)            freq[arr[i][0].charCodeAt() - 'a'.charCodeAt()]++;           // To store the count of required strings        let cnt = 0;           for (let i = 0; i < n; i++)        {               // Current word            let st = arr[i];               // Frequency array to store the frequency            // of each of the character            // of the current string            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;               // Check if the frequency of every character in            // the current string is <= its value in freq[]            for (let j = 1; j < 26; j++)            {                if (num[j] > freq[j])                {                    flag = false;                    break;                }            }               // First character of the current string            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>
2
 
ankthon
SURENDRA_GANGWAR
sanjeev2552
mukesh07
frequency-counting
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 50 String Coding Problems for Interviews
Vigenère Cipher
How to Append a Character to a String in C
Convert character array to string in C++
Naive algorithm for Pattern Searching
Return maximum occurring character in an input string
Hill Cipher
sprintf() in C
Program to add two binary strings
A Program to check if strings are rotations of each other or not