给定一个由小写英文字母组成的字符串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,形成2D数组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
// subsequnce 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
// subsequnce 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
# subsequnce 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
// subsequnce 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是给定字符串的长度。