📌  相关文章
📜  给定字符串中最大出现子序列的频率

📅  最后修改于: 2021-10-26 02:28:06             🧑  作者: Mango

给定一个由小写英文字母组成的字符串str ,我们的任务是找到出现次数最多的字符串序列的出现频率。

例子:

方法:该问题可以使用动态规划解决。为了解决上述问题,关键观察结果是生成的子序列的长度为1 或 2,因为任何长度 > 2 的子序列的频率都将低于长度为1 或 2的子序列,因为它们也存在于更长的子序列中.所以我们只需要检查长度为 1 或 2 的子序列。以下是步骤:

  • 对于长度 1,计算字符串中每个字母的频率。
  • 对于长度为 2 的二维数组dp[26][26] ,其中 dp[i][j] 告诉char(‘a’ + i) + char(‘a’ + j)字符串的频率。
  • 步骤 2 中使用的递推关系由下式给出:
  • 频率数组和数组dp[][]的最大值给出给定字符串中任何子序列的最大计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#define ll long long
using namespace std;
 
// Function to find the frequency
ll findCount(string s)
{
    // freq stores frequency of each
    // english lowercase character
    ll freq[26];
 
    // dp[i][j] stores the count of
    // subsequence with 'a' + i
    // and 'a' + j character
    ll dp[26][26];
 
    memset(freq, 0, sizeof freq);
 
    // Initialize dp to 0
    memset(dp, 0, sizeof dp);
 
    for (int i = 0; i < s.size(); ++i) {
 
        for (int j = 0; j < 26; j++) {
 
            // Increment the count of
            // subsequence j and s[i]
            dp[j][s[i] - 'a'] += freq[j];
        }
 
        // Update the frequency array
        freq[s[i] - 'a']++;
    }
 
    ll ans = 0;
 
    // For 1 length subsequence
    for (int i = 0; i < 26; i++)
        ans = max(freq[i], ans);
 
    // For 2 length subsequence
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
 
            ans = max(dp[i][j], ans);
        }
    }
 
    // Return the final result
    return ans;
}
 
// Driver Code
int main()
{
    // Given string str
    string str = "acbab";
 
    // Function Call
    cout << findCount(str);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the frequency
static int findCount(String s)
{
     
    // freq stores frequency of each
    // english lowercase character
    int []freq = new int[26];
 
    // dp[i][j] stores the count of
    // subsequence with 'a' + i
    // and 'a' + j character
    int [][]dp = new int[26][26];
 
    for(int i = 0; i < s.length(); ++i)
    {
        for(int j = 0; j < 26; j++)
        {
 
            // Increment the count of
            // subsequence j and s[i]
            dp[j][s.charAt(i) - 'a'] += freq[j];
        }
 
        // Update the frequency array
        freq[s.charAt(i) - 'a']++;
    }
 
    int ans = 0;
 
    // For 1 length subsequence
    for(int i = 0; i < 26; i++)
        ans = Math.max(freq[i], ans);
 
    // For 2 length subsequence
    for(int i = 0; i < 26; i++)
    {
        for(int j = 0; j < 26; j++)
        {
            ans = Math.max(dp[i][j], ans);
        }
    }
 
    // Return the final result
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String str
    String str = "acbab";
 
    // Function call
    System.out.print(findCount(str));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
import numpy
 
# Function to find the frequency
def findCount(s):
 
    # freq stores frequency of each
    # english lowercase character
    freq = [0] * 26
 
    # dp[i][j] stores the count of
    # subsequence with 'a' + i
    # and 'a' + j character
    dp = [[0] * 26] * 26
     
    freq = numpy.zeros(26)
    dp = numpy.zeros([26, 26])
 
    for i in range(0, len(s)):
        for j in range(26):
 
            # Increment the count of
            # subsequence j and s[i]
            dp[j][ord(s[i]) - ord('a')] += freq[j]
         
        # Update the frequency array
        freq[ord(s[i]) - ord('a')] += 1
 
    ans = 0
 
    # For 1 length subsequence
    for i in range(26):
        ans = max(freq[i], ans)
         
    # For 2 length subsequence
    for i in range(0, 26):
        for j in range(0, 26):
            ans = max(dp[i][j], ans)
     
    # Return the final result
    return int(ans)
 
# Driver Code
 
# Given string str
str = "acbab"
 
# Function call
print(findCount(str))
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the frequency
static int findCount(String s)
{
     
    // freq stores frequency of each
    // english lowercase character
    int []freq = new int[26];
 
    // dp[i,j] stores the count of
    // subsequence with 'a' + i
    // and 'a' + j character
    int [,]dp = new int[26, 26];
 
    for(int i = 0; i < s.Length; ++i)
    {
        for(int j = 0; j < 26; j++)
        {
 
            // Increment the count of
            // subsequence j and s[i]
            dp[j, s[i] - 'a'] += freq[j];
        }
 
        // Update the frequency array
        freq[s[i] - 'a']++;
    }
 
    int ans = 0;
 
    // For 1 length subsequence
    for(int i = 0; i < 26; i++)
        ans = Math.Max(freq[i], ans);
 
    // For 2 length subsequence
    for(int i = 0; i < 26; i++)
    {
        for(int j = 0; j < 26; j++)
        {
            ans = Math.Max(dp[i, j], ans);
        }
    }
 
    // Return the readonly result
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String str
    String str = "acbab";
 
    // Function call
    Console.Write(findCount(str));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
3

时间复杂度: O(26*N) ,其中 N 是给定字符串的长度。

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