给定一个由小写字符组成的字符串S和一个整数K ,任务是通过最多增加K 个字符来找到可能由单个不同字符组成的子序列的最大长度。
例子:
Input: S = “acscbcca” K = 1
Output: 5
Explanation: Incrementing the character S[4] from ‘b’ to ‘c’, modifies the string to “acscccca”. Therefore, longest subsequence of same characters is “ccccc”. Length of the subsequence is 5.
Input: S = “adscassr”, K = 2
Output: 4
方法:这个给定的问题可以通过使用滑动窗口技术和排序来解决。请按照以下步骤解决此问题:
- 初始化变量,比如start 、 end和sum为0 ,它存储子序列的开始和结束索引,即滑动窗口的总和。
- 初始化一个变量,比如对 INT_MIN 的ans存储结果最长子序列的长度。
- 对给定的字符串S 进行排序。
- 在[0, N – 1]范围内遍历字符串并执行以下步骤:
- 将总和的值增加值(S[end] – ‘a’) 。
- 迭代一个循环,直到(sum + K) 的值小于(S[end] – ‘a’) * (end – start + 1)并执行以下步骤:
- 将总和的值减少 ( S[start] – ‘a’) 。
- 将start的值增加1 。
- 经过上述步骤后,最多可以使用K 个增量使[start, end]范围内的所有字符相等。因此,将ans的值更新为ans和(end – start + 1)的最大值。
- 完成以上步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum length
// of a subsequence of same characters
// after at most K increment operations
void maxSubsequenceLen(string S, int K)
{
// Store the size of S
int N = S.length();
int start = 0, end = 0;
// Sort the given string
sort(S.begin(), S.end());
// Stores the maximum length and the
// sum of the sliding window
int ans = INT_MIN, sum = 0;
// Traverse the string S
for (end = 0; end < N; end++) {
// Add the current character
// to the window
sum = sum + (S[end] - 'a');
// Decrease the window size
while (sum + K
< (S[end] - 'a') * (end - start + 1)) {
// Update the value of sum
sum = sum - (S[start] - 'a');
// Increment the value
// of start
start++;
}
// Update the maximum window size
ans = max(ans, end - start + 1);
}
// Print the resulant maximum
// length of the subsequence
cout << ans;
}
// Driver Code
int main()
{
string S = "acscbcca";
int K = 1;
maxSubsequenceLen(S, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.math.*;
import java.util.Arrays;
class GFG{
// Function to find the maximum length
// of a subsequence of same characters
// after at most K increment operations
static void maxSubsequenceLen(String s, int K)
{
// Store the size of s
int N = s.length();
int start = 0, end = 0;
// Sort the given string
//sort(S.begin(), S.end());
// convert input string to char array
char S[] = s.toCharArray();
// sort tempArray
Arrays.sort(S);
// Stores the maximum length and the
// sum of the sliding window
int ans = Integer.MIN_VALUE, sum = 0;
// Traverse the string S
for(end = 0; end < N; end++)
{
// Add the current character
// to the window
sum = sum + (S[end] - 'a');
// Decrease the window size
while (sum + K < (S[end] - 'a') *
(end - start + 1))
{
// Update the value of sum
sum = sum - (S[start] - 'a');
// Increment the value
// of start
start++;
}
// Update the maximum window size
ans = Math.max(ans, end - start + 1);
}
// Print the resulant maximum
// length of the subsequence
System.out.println(ans);
}
// Driver code
public static void main(String args[])
{
String S = "acscbcca";
int K = 1;
maxSubsequenceLen(S, K);
}
}
// This code is contributed by jana_sayantan
Python3
# Python3 program for the above approach
# Function to find the maximum length
# of a subsequence of same characters
# after at most K increment operations
def maxSubsequenceLen(S, K):
# Store the size of S
N = len(S)
start, end = 0, 0
# Sort the given string
S = sorted(S)
# Stores the maximum length and the
# sum of the sliding window
ans, sum =-10**9, 0
# Traverse the S
for end in range(N):
# Add the current character
# to the window
sum = sum + (ord(S[end]) - ord('a'))
# Decrease the window size
while (sum + K < (ord(S[end]) - ord('a')) *
(end - start + 1)):
# Update the value of sum
sum = sum - (ord(S[start]) - ord('a'))
# Increment the value
# of start
start += 1
# Update the maximum window size
ans = max(ans, end - start + 1)
# Print the resulant maximum
# length of the subsequence
print (ans)
# Driver Code
if __name__ == '__main__':
S = "acscbcca"
K = 1
maxSubsequenceLen(S, K)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find the maximum length
// of a subsequence of same characters
// after at most K increment operations
static void maxSubsequenceLen(string s, int K)
{
// Store the size of s
int N = s.Length;
int start = 0, end = 0;
// Sort the given string
//sort(S.begin(), S.end());
// convert input string to char array
char[] S= s.ToCharArray();
// sort tempArray
Array.Sort(S);
// Stores the maximum length and the
// sum of the sliding window
int ans = Int32.MinValue, sum = 0;
// Traverse the string S
for(end = 0; end < N; end++)
{
// Add the current character
// to the window
sum = sum + (S[end] - 'a');
// Decrease the window size
while (sum + K < (S[end] - 'a') *
(end - start + 1))
{
// Update the value of sum
sum = sum - (S[start] - 'a');
// Increment the value
// of start
start++;
}
// Update the maximum window size
ans = Math.Max(ans, end - start + 1);
}
// Print the resulant maximum
// length of the subsequence
Console.WriteLine(ans);
}
// Driver Code
public static void Main()
{
string S = "acscbcca";
int K = 1;
maxSubsequenceLen(S, K);
}
}
// This code is contributed by souravghosh0416.
Javascript
输出:
5
时间复杂度: O(N * log N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live