给定一个由小写英文字母组成的字符串str ,我们的任务是找到出现次数最多的字符串序列的出现频率。
例子:
Input: s = “aba”
Output: 2
Explanation:
For “aba”, subsequence “ab” occurs maximum times in subsequence ‘ab’ and ‘aba’.
Input: s = “acbab”
Output: 3
Explanation:
For “acbab”, “ab” occurs 3 times which is the maximum.
方法:该问题可以使用动态规划解决。为了解决上述问题,关键观察结果是生成的子序列的长度为1 或 2,因为任何长度 > 2 的子序列的频率都将低于长度为1 或 2的子序列,因为它们也存在于更长的子序列中.所以我们只需要检查长度为 1 或 2 的子序列。以下是步骤:
- 对于长度 1,计算字符串中每个字母的频率。
- 对于长度为 2 的二维数组dp[26][26] ,其中 dp[i][j] 告诉char(‘a’ + i) + char(‘a’ + j)字符串的频率。
- 步骤 2 中使用的递推关系由下式给出:
dp[i][j] = dp[i][j] + freq[i]
where,
freq[i] = frequency of character char(‘a’ + i)
dp[i][j] = frequency of string formed by current_character + char(‘a’ + i).
- 频率数组和数组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 frequnecy 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);
// Intialize 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 frequnecy 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 frequnecy 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 frequnecy 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
3
时间复杂度: O(26*N) ,其中 N 是给定字符串的长度。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live