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

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

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

给定一个字符串和一个数字 k,找到一个字符串中每个字符至少出现 k 次的最长子序列。

例子:

Input : str = "geeksforgeeks"
         k = 2
Output : geeksgeeks
Every character in the output
subsequence appears at-least 2
times.

Input : str = "aabbaabacabb"
          k = 5
Output : aabbaabaabb

方法1(蛮力)
我们生成所有子序列。对于每个子序列,计算其中的不同字符,并找到每个字符至少出现 k 次的最长子序列。

方法2(高效方式)
1. 找到字符串的频率并将其存储在一个大小为 26 的整数数组中,表示字母。
2.找到频率后逐个字符地迭代字符串,如果该字符的频率大于或等于所需的字符。

C++
// C++ program to Find longest subsequence where
//  every character appears at-least k times
#include
using namespace std;
 
const int MAX_CHARS = 26;
 
void longestSubseqWithK(string str, int k)   
{
    int n = str.size();                  
 
    // Count frequencies of all characters
    int freq[MAX_CHARS] = {0};                   
    for (int i = 0 ; i < n; i++)   
        freq[str[i] - 'a']++;             
     
    // Traverse given string again and print
    // all those characters whose frequency
    // is more than or equal to k.
    for (int i = 0 ; i < n ; i++)  
        if (freq[str[i] - 'a'] >= k)              
            cout << str[i];     
}
 
// Driver code
int main() {
    string str = "geeksforgeeks";
    int k = 2;
    longestSubseqWithK(str, k);      
    return 0;
}


Java
// Java program to Find longest subsequence where
//  every character appears at-least k times
 
class GFG {
 
    static final int MAX_CHARS = 26;
 
    static void longestSubseqWithK(String str, int k) {
        int n = str.length();
 
        // Count frequencies of all characters
        int freq[] = new int[MAX_CHARS];
        for (int i = 0; i < n; i++) {
            freq[str.charAt(i) - 'a']++;
        }
 
        // Traverse given string again and print
        // all those characters whose frequency
        // is more than or equal to k.
        for (int i = 0; i < n; i++) {
            if (freq[str.charAt(i) - 'a'] >= k) {
                System.out.print(str.charAt(i));
            }
        }
    }
 
// Driver code
    static public void main(String[] args) {
        String str = "geeksforgeeks";
        int k = 2;
        longestSubseqWithK(str, k);
 
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to Find longest subsequence where
#  every character appears at-least k times
  
MAX_CHARS = 26
  
def longestSubseqWithK(s, k):   
 
    n = len(s)                 
  
    # Count frequencies of all characters
    freq = [0]*MAX_CHARS                
    for i in range(n): 
        freq[ord(s[i]) - ord('a')]+=1             
      
    # Traverse given string again and print
    # all those characters whose frequency
    # is more than or equal to k.
    for i in range(n ):
        if (freq[ord(s[i]) - ord('a')] >= k):              
            print(s[i],end="")
  
# Driver code
if __name__ == "__main__":
     
    s = "geeksforgeeks"
    k = 2
    longestSubseqWithK(s, k)


C#
// C# program to Find longest subsequence where
//  every character appears at-least k times
using System;
public class GFG {
  
    static readonly int MAX_CHARS = 26;
  
    static void longestSubseqWithK(String str, int k) {
        int n = str.Length;
  
        // Count frequencies of all characters
        int []freq = new int[MAX_CHARS];
        for (int i = 0; i < n; i++) {
            freq[str[i]- 'a']++;
        }
  
        // Traverse given string again and print
        // all those characters whose frequency
        // is more than or equal to k.
        for (int i = 0; i < n; i++) {
            if (freq[str[i] - 'a'] >= k) {
                Console.Write(str[i]);
            }
        }
    }
  
// Driver code
    static public void Main() {
        String str = "geeksforgeeks";
        int k = 2;
        longestSubseqWithK(str, k);
  
    }
}
  
// This code is contributed by Rajput-Ji


Javascript


C++
// C++ program to Find longest subsequence where every
// character appears at-least k times
#include 
using namespace std;
 
void longestSubseqWithK(string str, int k)
{
  int n = str.size();
  map hm;
 
  // Count frequencies of all characters
  for (int i = 0; i < n; i++) {
    char c = str[i];
    hm++;
  }
 
  // Traverse given string again and print
  // all those characters whose frequency
  // is more than or equal to k.
  for (int i = 0; i < n; i++) {
    char c = str[i];
    if (hm >= k) {
      cout << c;
    }
  }
}
 
// Driver code
int main()
{
  string str = "geeksforgeeks";
  int k = 2;
  longestSubseqWithK(str, k);
 
  return 0;
}
 
// This code is contributed by rakeshsahni


Java
/*package whatever //do not write package name here */
// Java program to Find longest subsequence where every
// character appears at-least k times
 
import java.io.*;
import java.util.HashMap;
 
class GFG {
 
    static void longestSubseqWithK(String str, int k)
    {
        int n = str.length();
        HashMap hm = new HashMap<>();
 
        // Count frequencies of all characters
        for (int i = 0; i < n; i++) {
            char c = str.charAt(i);
            if (hm.containsKey(c))
                hm.put(c, hm.get(c) + 1);
            else
                hm.put(c, 1);
        }
 
        // Traverse given string again and print
        // all those characters whose frequency
        // is more than or equal to k.
        for (int i = 0; i < n; i++) {
            char c = str.charAt(i);
            if (hm.get(c) >= k) {
                System.out.print(c);
            }
        }
    }
 
    // Driver code
    static public void main(String[] args)
    {
        String str = "geeksforgeeks";
        int k = 2;
        longestSubseqWithK(str, k);
    }
}
 
// This code is contributed by Chandan-Bedi


输出
geeksgeeks

此代码的时间复杂度为 O(n),其中 n 是字符串的大小。

方法3(高效方式——使用HashMap)

C++

// C++ program to Find longest subsequence where every
// character appears at-least k times
#include 
using namespace std;
 
void longestSubseqWithK(string str, int k)
{
  int n = str.size();
  map hm;
 
  // Count frequencies of all characters
  for (int i = 0; i < n; i++) {
    char c = str[i];
    hm++;
  }
 
  // Traverse given string again and print
  // all those characters whose frequency
  // is more than or equal to k.
  for (int i = 0; i < n; i++) {
    char c = str[i];
    if (hm >= k) {
      cout << c;
    }
  }
}
 
// Driver code
int main()
{
  string str = "geeksforgeeks";
  int k = 2;
  longestSubseqWithK(str, k);
 
  return 0;
}
 
// This code is contributed by rakeshsahni

Java

/*package whatever //do not write package name here */
// Java program to Find longest subsequence where every
// character appears at-least k times
 
import java.io.*;
import java.util.HashMap;
 
class GFG {
 
    static void longestSubseqWithK(String str, int k)
    {
        int n = str.length();
        HashMap hm = new HashMap<>();
 
        // Count frequencies of all characters
        for (int i = 0; i < n; i++) {
            char c = str.charAt(i);
            if (hm.containsKey(c))
                hm.put(c, hm.get(c) + 1);
            else
                hm.put(c, 1);
        }
 
        // Traverse given string again and print
        // all those characters whose frequency
        // is more than or equal to k.
        for (int i = 0; i < n; i++) {
            char c = str.charAt(i);
            if (hm.get(c) >= k) {
                System.out.print(c);
            }
        }
    }
 
    // Driver code
    static public void main(String[] args)
    {
        String str = "geeksforgeeks";
        int k = 2;
        longestSubseqWithK(str, k);
    }
}
 
// This code is contributed by Chandan-Bedi
输出
geeksgeeks

时间复杂度: O(n) 其中 n 是字符串的大小。