给定长度为N 的字符串str ,任务是通过将任何一对不相等的相邻字符替换为最多K次的单个字符来找到给定字符串可以减少到的最小长度。
例子:
Input: str = “aabc”, K =1
Output: 3
Explanation:
Replace “bc” with a single character “d” and decrement K by 1.
Therefore, str modifies to “aad”.
Since K = 0, the minimized length of the string is 3.
Input: str= “aabc”, K = 2
Output: 2
朴素的方法:最简单的方法是遍历字符串K次,在每次遍历期间,检查给定字符串的任何相邻字符对是否不同。如果发现为真,则更换都与一个单个字符不等于与其相邻的字符的字符。最后,打印字符串的最小长度。
时间复杂度: O(N * K)
辅助空间: O(1)
高效方法:为了优化上述方法,这个想法是遍历给定的字符串和检查是否有任何一对给定的字符串的相邻字符是不同的或没有。如果发现为真,则打印max(1, (N – K)) 的值。否则,打印N 。请按照以下步骤解决问题:
- 遍历给定的字符串str并检查任何相邻字符对是否不同。
- 如果发现为真,则打印max(1, (N – K))作为所需答案。
- 否则,打印N作为所需答案。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to minimize the length
// of the string by replacing distinct
// pairs of adjacent characters
int MinLen(string str, int K)
{
// Stores the length
// of the given string
int N = str.length();
// Stores the index
// of the given string
int i = 0;
// Traverse the given string
while (i < N - 1) {
// If two adjacent
// characters are distinct
if (str[i] != str[i + 1]) {
break;
}
i++;
}
// If all characters
// are equal
if (i == N - 1) {
return N;
}
// If all characters
// are distinct
return max(1, N - K);
}
// Driver Code
int main()
{
string str = "aabc";
int K = 1;
cout << MinLen(str, K);
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to minimize the
// length of the String by
// replacing distinct pairs
// of adjacent characters
static int MinLen(String str,
int K)
{
// Stores the length
// of the given String
int N = str.length();
// Stores the index
// of the given String
int i = 0;
// Traverse the given String
while (i < N - 1)
{
// If two adjacent
// characters are distinct
if (str.charAt(i) !=
str.charAt(i + 1))
{
break;
}
i++;
}
// If all characters
// are equal
if (i == N - 1)
{
return N;
}
// If all characters
// are distinct
return Math.max(1, N - K);
}
// Driver Code
public static void main(String[] args)
{
String str = "aabc";
int K = 1;
System.out.print(MinLen(str, K));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to minimize the length
# of the by replacing distinct
# pairs of adjacent characters
def MinLen(str, K):
# Stores the length
# of the given string
N = len(str)
# Stores the index
# of the given string
i = 0
# Traverse the given string
while (i < N - 1):
# If two adjacent
# haracters are distinct
if (str[i] != str[i + 1]):
break
i += 1
# If all characters
# are equal
if (i == N - 1):
return N
# If all characters
# are distinct
return max(1, N - K)
# Driver Code
if __name__ == '__main__':
str = "aabc"
K = 1
print(MinLen(str, K))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to minimize the
// length of the String by
// replacing distinct pairs
// of adjacent characters
static int MinLen(String str,
int K)
{
// Stores the length
// of the given String
int N = str.Length;
// Stores the index
// of the given String
int i = 0;
// Traverse the given String
while (i < N - 1)
{
// If two adjacent
// characters are distinct
if (str[i] != str[i + 1])
{
break;
}
i++;
}
// If all characters
// are equal
if (i == N - 1)
{
return N;
}
// If all characters
// are distinct
return Math.Max(1, N - K);
}
// Driver Code
public static void Main(String[] args)
{
String str = "aabc";
int K = 1;
Console.Write(MinLen(str, K));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。