📜  首字母缩略词

📅  最后修改于: 2022-05-13 01:57:07.415000             🧑  作者: Mango

首字母缩略词

给定N个由英文字母的小写字母组成的字符串。任务是找出这N个字符串中有多少个字符串是其他N-1 个字符串的首字母缩写词。
对于字符串的子集,我们可以选择以任何方式对它们进行排序,然后将它们的第一个字母连接起来。例如, csa是子集{computer, academy, science}的首字母缩写词, acs也是如此。打印可以是其他字符串的首字母缩写词的字符串数。
例子:

方法:假设我们有一个频率数组freq ,其中freq[i]是字符i在给定字符串中的第一个的次数。为了检查字符串S是否可以是首字母缩写词,首先,我们应该减少S的第一个字母的频率,然后检查S中每个字母的频率是否小于或等于它在freq[]数组中的值。
下面是上述方法的实现:

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


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


Javascript


输出:
2