📌  相关文章
📜  通过单个子序列的每个索引部分最大化3长度回文子序列的计数

📅  最后修改于: 2021-04-17 14:19:54             🧑  作者: Mango

给定字符串S ,任务是从给定的字符串找到最大长度为3的不同索引回文子序列的最大数目。

例子:

方法:我们的想法是计数字符串S的每个字符的频率,和计数的频率对,使得对是相同的字符,并通过除以3字符串S计数长度为3的子序列的数目。最后,将频率对的最小值打印为子序列数。请按照以下步骤解决问题:

  • 初始化一个数组,例如freq [] ,以存储字符串S的每个字符的频率。
  • 初始化一个变量,例如freqPair ,以存储具有相同字符对的频率对。
  • 初始化一个变量len ,以存储字符串S的长度3的子字符串。
  • 迭代范围[0,str.length()– 1] 。对于字符串S的i索引,将字符freq [S [i] –’a’]的计数增加1
  • 迭代范围[ 0,26 ] 。对于数组freq []的i索引,通过将数组元素除以2来计算频率对。
  • 最后,打印freqPairlen的最小值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count the maximum number
// oaf palindrome subsequences of length 3
// considering the same index only once
int maxNumPalindrome(string S)
{
 
    // Index of the string S
    int i = 0;
 
    // Stores the frequency of
    // every character
    int freq[26] = { 0 };
 
    // Stores the pair of frequency
    // containing same characters
    int freqPair = 0;
 
    // Number of subsequences
    // having length 3
    int len = S.length() / 3;
 
    // Counts the frequency
    while (i < S.length()) {
 
        freq[S[i] - 'a']++;
        i++;
    }
 
    // Counts the pair of frequency
    for (i = 0; i < 26; i++) {
 
        freqPair += (freq[i] / 2);
    }
 
    // Returns the minimum value
    return min(freqPair, len);
}
 
// Driver Code
int main()
{
 
    string S = "geeksforg";
 
    cout << maxNumPalindrome(S) << endl;
 
    return 0;
}


Java
// Java program to implement
// the above approach
 
import java.util.*;
 
class GFG {
   
    // Driver Code
    public static void main(String[] args)
    {
 
        String S = "geeksforg";
 
        System.out.println(maxNumPalindrome(S));
    }
 
    // Function to count the maximum number
    // of palindrome subsequences of length 3
    // considering the same index only once
    static int maxNumPalindrome(String S)
    {
 
        // Index of the string S
        int i = 0;
 
        // Stores the frequency of
        // every character
        int[] freq = new int[26];
 
        // Stores the pair of frequency
        // containing same characters
        int freqPair = 0;
 
        // Number of subsequences
        // having length 3
        int len = S.length() / 3;
 
        // Counts the frequency
        while (i < S.length()) {
 
            freq[S.charAt(i) - 'a']++;
            i++;
        }
 
        // Counts the pair of frequency
        for (i = 0; i < 26; i++) {
 
            freqPair += (freq[i] / 2);
        }
 
        // Returns the minimum value
        return Math.min(freqPair, len);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to count the maximum number
# of palindrome subsequences of length 3
# considering the same index only once
def maxNumPalindrome(S):
     
    # Index of the S
    i = 0
 
    # Stores the frequency of
    # every character
    freq = [0] * 26
 
    # Stores the pair of frequency
    # containing same characters
    freqPair = 0
 
    # Number of subsequences
    # having length 3
    ln = len(S) // 3
 
    # Counts the frequency
    while (i < len(S)):
        freq[ord(S[i]) - ord('a')] += 1
        i += 1
 
    # Counts the pair of frequency
    for i in range(26):
        freqPair += (freq[i] // 2)
 
    # Returns the minimum value
    return min(freqPair, ln)
 
# Driver Code
if __name__ == '__main__':
 
    S = "geeksforg"
 
    print(maxNumPalindrome(S))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach 
using System;
class GFG
{
   
    // Driver Code
    public static void Main(String[] args)
    {
        string S = "geeksforg";
        Console.WriteLine(maxNumPalindrome(S));
    }
 
    // Function to count the maximum number
    // of palindrome subsequences of length 3
    // considering the same index only once
    static int maxNumPalindrome(string S)
    {
 
        // Index of the string S
        int i = 0;
 
        // Stores the frequency of
        // every character
        int[] freq = new int[26];
 
        // Stores the pair of frequency
        // containing same characters
        int freqPair = 0;
 
        // Number of subsequences
        // having length 3
        int len = S.Length / 3;
 
        // Counts the frequency
        while (i < S.Length)
        {
            freq[S[i] - 'a']++;
            i++;
        }
 
        // Counts the pair of frequency
        for (i = 0; i < 26; i++)
        {
            freqPair += (freq[i] / 2);
        }
 
        // Returns the minimum value
        return Math.Min(freqPair, len);
    }
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
2

时间复杂度: O(| S | + 26)
辅助空间: O(26)