📌  相关文章
📜  每个字符串至少出现一个字符的最长子序列

📅  最后修改于: 2021-10-27 08:39:45             🧑  作者: Mango

给定一个字符串数组arr[] ,任务是找到该数组的最长子序列,其中至少有一个字符出现在所有字符串。请注意,所有字符串仅包含小写英文字母。
例子:

方法:创建一个count[]数组,这样count[0]将存储包含‘a’的字符串数, count[1]将存储包含‘b’的字符串数,依此类推……
现在,很明显答案将是count[]数组中的最大值。为了更新这个数组,开始遍历字符串数组,对于每个字符串,在hash[]数组中标记当前字符串中存在哪些字符。
遍历之后,对于当前字符串存在的每个字符,更新它在count[]数组中的计数
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
#define MAX 26
 
// Function to return the length of the longest
// sub-sequence with at least one
// common character in every string
int largestSubSeq(string arr[], int n)
{
 
    // count[0] will store the number of strings
    // which contain 'a', count[1] will store the
    // number of strings which contain 'b' and so on..
    int count[MAX] = { 0 };
 
    // For every string
    for (int i = 0; i < n; i++) {
        string str = arr[i];
 
        // Hash array to set which character is
        // present in the current string
        bool hash[MAX] = { 0 };
        for (int j = 0; j < str.length(); j++) {
            hash[str[j] - 'a'] = true;
        }
 
        for (int j = 0; j < MAX; j++) {
 
            // If current character appears in the
            // string then update its count
            if (hash[j])
                count[j]++;
        }
    }
 
    return *(max_element(count, count + MAX));
}
 
// Driver code
int main()
{
    string arr[] = { "ab", "bc", "de" };
    int n = sizeof(arr) / sizeof(string);
 
    cout << largestSubSeq(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
         
    static int MAX = 26;
     
    // Function to return the length of the longest
    // sub-sequence with at least one
    // common character in every string
    static int largestSubSeq(String arr[], int n)
    {
     
        // count[0] will store the number of strings
        // which contain 'a', count[1] will store the
        // number of strings which contain 'b' and so on..
        int [] count = new int[MAX];
     
        // For every string
        for (int i = 0; i < n; i++) {
            String str = arr[i];
     
            // Hash array to set which character is
            // present in the current string
            boolean [] hash = new boolean[MAX];
             
             
            for (int j = 0; j < str.length(); j++) {
                hash[str.charAt(j) - 'a'] = true;
            }
     
            for (int j = 0; j < MAX; j++) {
     
                // If current character appears in the
                // string then update its count
                if (hash[j])
                    count[j]++;
            }
        }
         
        int max = -1;
     
        for(int i=0;i< MAX; i++)
        {
            if(max < count[i])
                max = count[i];
        }
        return max;
    }
     
    // Driver code
    public static void main (String[] args)
    {
         
        String arr[] = { "ab", "bc", "de" };
        int n = arr.length;
     
        System.out.println(largestSubSeq(arr, n));
     
 
    }
 
 
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
MAX = 26
 
# Function to return the length of the longest
# sub-sequence with at least one
# common character in every string
def largestSubSeq(arr, n):
     
    # count[0] will store the number of strings
    # which contain 'a', count[1] will store the
    # number of strings which contain 'b' and so on..
    count = [0] * MAX
     
    # For every string
    for i in range(n):
        string = arr[i]
         
        # Hash array to set which character is
        # present in the current string
        _hash = [False] * MAX
        for j in range(len(string)):
            _hash[ord(string[j]) - ord('a')] = True
         
        for j in range(MAX):
             
            # If current character appears in the
            # string then update its count
            if _hash[j] == True:
                count[j] += 1
                 
    return max(count)
 
# Driver code
if __name__ == "__main__":
    arr = [ "ab", "bc", "de" ]
    n = len(arr)
    print(largestSubSeq(arr, n))
 
# This code is contributed by
# sanjeev2552


C#
// C# implementation of the approach
using System;
 
class GFG
{
         
    static int MAX = 26;
     
    // Function to return the length of the longest
    // sub-sequence with at least one
    // common character in every string
    static int largestSubSeq(string [] arr, int n)
    {
     
        // count[0] will store the number of strings
        // which contain 'a', count[1] will store the
        // number of strings which contain 'b' and so on..
        int [] count = new int[MAX];
     
        // For every string
        for (int i = 0; i < n; i++)
        {
            string str = arr[i];
     
            // Hash array to set which character is
            // present in the current string
            bool [] hash = new bool[MAX];
             
             
            for (int j = 0; j < str.Length; j++)
            {
                hash[str[j] - 'a'] = true;
            }
     
            for (int j = 0; j < MAX; j++)
            {
     
                // If current character appears in the
                // string then update its count
                if (hash[j])
                    count[j]++;
            }
        }
         
        int max = -1;
     
        for(int i=0;i< MAX; i++)
        {
            if(max < count[i])
                max = count[i];
        }
        return max;
    }
     
    // Driver code
    public static void Main ()
    {
         
        string [] arr = { "ab", "bc", "de" };
        int n = arr.Length;
     
        Console.WriteLine(largestSubSeq(arr, n));
    }
}
 
// This code is contributed by ihritik


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程