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

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

每个字符至少出现 k 次的最长子序列

给定一个字符串's' 和一个整数 k,找到另一个字符串't' 使得 't' 是给定字符串's' 的最大子序列,并且 't' 的每个字符必须在字符串s 中至少出现 k 次。
例子 :

Input : s = "geeksforgeeks"
        k = 2
Output : geeksgeeks

Input : s = "baaabaacba"
        k = 3
Output : baaabaaba

一个简单的解决方案是生成所有子序列。对于每个子序列,检查它是否包含所有字符至少 k 次。找到最长的这样的子序列。这种方法的时间复杂度是指数级的。
有效的方法我们可以用另一个数组来记录字符串s中每个字符的计数,如果任何字符出现超过或等于k次,那么我们就简单地打印它。

CPP
// CPP Program to find the subsequence
// with each character occurring at least
// k times in string s
#include 
using namespace std;
 
#define MAX_CHAR 26
 
// Function to find the subsequence
void findSubsequence(string str, int k)
{
    // Taking an extra array to keep
    // record for character count in s
    int a[MAX_CHAR] = { 0 };
 
    // Counting occurrences of all
    // characters in str[]
    for (int i = 0; i < str.size(); i++)
        a[str[i] - 'a']++;   
 
    // Printing characters with count
    // >= k in same order as they appear
    // in str.
    for (int i = 0; i < l; i++)
        if (a[str[i] - 'a'] >= k)
            cout << str[i];   
}
 
// Driver code
int main()
{
    int k = 2;
    findSubsequence("geeksforgeeks", k);
    return 0;
}


Java
// Java Program to find the subsequence
// with each character occurring at least
// k times in string s
class GFG {
     
    static final int MAX_CHAR = 26;
     
    // Function to find the subsequence
    static void findSubsequence(String str, int k)
    {
         
        // Taking an extra array to keep
        // record for character count in s
        int a[] = new int[MAX_CHAR];
     
        // Counting occurrences of all
        // characters in str[]
        for (int i = 0; i < str.length(); i++)
            a[str.charAt(i) - 'a']++;
     
        // Printing characters with count
        // >= k in same order as they appear
        // in str.
        for (int i = 0; i < str.length(); i++)
            if (a[str.charAt(i) - 'a'] >= k)
                System.out.print(str.charAt(i));
    }
     
    // Driver code
    public static void main(String[] args) {
         
        int k = 2;
        findSubsequence("geeksforgeeks", k);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python Program to find the subsequence
# with each character occurring at least
# k times in string s
 
MAX_CHAR = 26
 
# Function to find the subsequence
def findSubsequence(stri, k):
    # Taking an extra array to keep
    # record for character count in s
    a = [0] * MAX_CHAR;
 
    # Counting occurrences of all
    # characters in str[]
    for i in range(len(stri)):
        a[ord(stri[i]) - ord('a')] += 1
 
    # Printing characters with count
    # >= k in same order as they appear
    # in str.
    for i in range(len(stri)):
        if a[ord(stri[i]) - ord('a')] >= k:
            print(stri[i],end='')
 
# Driver code
k = 2
findSubsequence("geeksforgeeks", k)
 
# This code is contributed by Shubham Rana


C#
// C# Program to find the subsequence
// with each character occurring at
// least k times in string s
using System;
 
class GFG {
     
    static int MAX_CHAR = 26;
     
    // Function to find the subsequence
    static void findSubsequence(string str, int k)
    {
         
        // Taking an extra array to keep
        // record for character count in s
        int []a = new int[MAX_CHAR];
     
        // Counting occurrences of all
        // characters in str[]
        for (int i = 0; i < str.Length; i++)
            a[str[i] - 'a']++;
     
        // Printing characters with count
        // >= k in same order as they appear
        // in str.
        for (int i = 0; i < str.Length; i++)
            if (a[str[i] - 'a'] >= k)
                Console.Write(str[i]);
    }
     
    // Driver code
    public static void Main() {
         
        int k = 2;
        findSubsequence("geeksforgeeks", k);
    }
}
 
// This code is contributed by vt_m.


PHP
= k in same order
    // as they appear in str.
    for ($i = 0; $i < strlen($str); $i++)
        if ($a[ord($str[$i]) - ord('a')] >= $k)
            echo $str[$i];
}
 
// Driver code
$k = 2;
findSubsequence("geeksforgeeks", $k);
     
// This code is contributed by Sam007
?>


Javascript


输出:

geeksgeeks