给定字符串S ,任务是仅使用索引在“几何级数”中的那些字符从S中找到最大出现子序列P的计数。
注意:考虑S中基于1的索引。
例子 :
Input: S = “ddee”
Output: 4
Explanation:
If we take P = “de”, then P occurs 4 times in S. { {1, 3}, {1, 4}, {2, 3}, {2, 4} }
Input: S = “geeksforgeeks”
Output: 6
Explanation:
If we take P = “ek”, then P occurs 6 times in S. { {2, 4}, {3, 4}, {2, 12} {3, 12}, {10, 12}, {11, 12} }
原始的方法:我们的想法是产生给定字符串的所有可能的子序列,以使字符串的指标必须是几何级数。现在,对于生成的每个子序列,找到每个子序列的出现,并在这些出现中打印最大值。
时间复杂度: O(2 N )
辅助空间: O(1)
高效的方法:这个想法是要观察到任何子序列P可以具有任何长度。假设如果P =“ abc”,并且它在S中出现10次(其中“ abc”在S中的GP中有其索引),那么我们可以看到子序列“ ab”(在GP中具有索引)也将在10中出现10次S。因此,为了简化解决方案,P的可能长度将小于等于2。以下是步骤:
- 必须选择长度大于1的子序列P ,因为如果S不只包含唯一字符,则长度大于1的P会比长度1的时间长得多。
- 对于长度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
using namespace std;
// Function to count maximum occurring
// subsequence using only those characters
// whose indexes are in GP
int findMaxTimes(string S)
{
long long int arr[26];
long long int dp[26][26];
// Initialize 1-D array and 2-D
// dp array to 0
memset(arr, 0, sizeof(arr));
memset(dp, 0, sizeof(dp));
// Iterate till the length of
// the given string
for (int i = 0; i < S.size(); i++) {
int now = S[i] - 'a';
for (int j = 0; j < 26; j++) {
dp[j][now] += arr[j];
}
arr[now]++;
}
long long int ans = 0;
// Update ans for 1-length subsequence
for (int i = 0; i < 26; i++)
ans = max(ans, arr[i]);
// Update ans for 2-length subsequence
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
ans = max(ans, dp[i][j]);
}
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
// Given string s
string S = "ddee";
// Function Call
cout << findMaxTimes(S);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count maximum occurring
// subsequence using only those characters
// whose indexes are in GP
static int findMaxTimes(String S)
{
int []arr = new int[26];
int [][]dp = new int[26][26];
// Iterate till the length of
// the given String
for(int i = 0; i < S.length(); i++)
{
int now = S.charAt(i) - 'a';
for(int j = 0; j < 26; j++)
{
dp[j][now] += arr[j];
}
arr[now]++;
}
int ans = 0;
// Update ans for 1-length subsequence
for(int i = 0; i < 26; i++)
ans = Math.max(ans, arr[i]);
// Update ans for 2-length subsequence
for(int i = 0; i < 26; i++)
{
for(int j = 0; j < 26; j++)
{
ans = Math.max(ans, dp[i][j]);
}
}
// Return the answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given String s
String S = "ddee";
// Function call
System.out.print(findMaxTimes(S));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to count maximum occurring
# subsequence using only those characters
# whose indexes are in GP
def findMaxTimes(S):
# Initialize 1-D array and 2-D
# dp array to 0
arr = [0] * 26
dp = [[0 for x in range(26)]
for y in range(26)]
# Iterate till the length of
# the given string
for i in range(len(S)):
now = ord(S[i]) - ord('a')
for j in range(26):
dp[j][now] += arr[j]
arr[now] += 1
ans = 0
# Update ans for 1-length subsequence
for i in range(26):
ans = max(ans, arr[i])
# Update ans for 2-length subsequence
for i in range(26):
for j in range(26):
ans = max(ans, dp[i][j])
# Return the answer
return ans
# Driver Code
# Given string s
S = "ddee"
# Function call
print(findMaxTimes(S))
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
class GFG{
// Function to count maximum occurring
// subsequence using only those characters
// whose indexes are in GP
static int findMaxTimes(String S)
{
int []arr = new int[26];
int [,]dp = new int[26, 26];
// Iterate till the length of
// the given String
for(int i = 0; i < S.Length; i++)
{
int now = S[i] - 'a';
for(int j = 0; j < 26; j++)
{
dp[j, now] += arr[j];
}
arr[now]++;
}
int ans = 0;
// Update ans for 1-length subsequence
for(int i = 0; i < 26; i++)
ans = Math.Max(ans, arr[i]);
// Update ans for 2-length subsequence
for(int i = 0; i < 26; i++)
{
for(int j = 0; j < 26; j++)
{
ans = Math.Max(ans, dp[i, j]);
}
}
// Return the answer
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Given String s
String S = "ddee";
// Function call
Console.Write(findMaxTimes(S));
}
}
// This code is contributed by gauravrajput1
4
时间复杂度: O(max(N * 26,26 * 26))
辅助空间: O(26 * 26)