给定一个由小写字符组成的字符串str ,任务是修改字符串,使其不包含任何长度超过1的回文子字符串,通过最少的字符替换。
例子:
Input: str = “bbbbbbb”
Output: 4
String can be modified to “bacbacb” by replacing 4 characters.
Input: str = “geeksforgeeks”
Output: 2
方法:
解决这个问题的思路是,如果存在长度大于3的回文,则存在长度为2或3的回文。因此,贪婪地删除所有长度为2或3的回文。按照以下步骤解决问题:
- 初始化一个变量,比如change ,以存储所需的替换次数。
- 迭代指定字符串的字符,请执行以下步骤:
- 如果当前索引处的字符是相同的字符的下一个索引处,然后由1个增量变化。
- 否则,检查如果先前索引处的字符是相同的字符的下一个索引处,即,存在长度为3的回文串。因此,将 change增加 1。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to count the changes required such
// that no palindromic substring of length
// exceeding 1 is present in the string
int maxChange(string str)
{
// Base Case
if (str.size() <= 1) {
return 0;
}
// Stores the count
int minChanges = 0;
// Iterate over the string
for (int i = 0; i < str.size() - 1; i++) {
// Palindromic Substring of Length 2
if (str[i] == str[i + 1]) {
// Replace the next character
str[i + 1] = 'N';
// Increment changes
minChanges += 1;
}
// Palindromic Substring of Length 3
else if (i > 0 && str[i - 1] == str[i + 1]) {
// Replace the next character
str[i + 1] = 'N';
// Increment changes
minChanges += 1;
}
}
return minChanges;
}
// Driver Code
int main()
{
string str = "bbbbbbb";
cout << maxChange(str);
return 0;
}
Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to count the changes required such
// that no palindromic subString of length
// exceeding 1 is present in the String
static int maxChange(char []str)
{
// Base Case
if (str.length <= 1)
{
return 0;
}
// Stores the count
int minChanges = 0;
// Iterate over the String
for (int i = 0; i < str.length - 1; i++)
{
// Palindromic SubString of Length 2
if (str[i] == str[i + 1])
{
// Replace the next character
str[i + 1] = 'N';
// Increment changes
minChanges += 1;
}
// Palindromic SubString of Length 3
else if (i > 0 && str[i - 1] == str[i + 1])
{
// Replace the next character
str[i + 1] = 'N';
// Increment changes
minChanges += 1;
}
}
return minChanges;
}
// Driver Code
public static void main(String[] args)
{
String str = "bbbbbbb";
System.out.print(maxChange(str.toCharArray()));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 Program to implement
# the above approach
# Function to count the changes required such
# that no palindromic subof length
# exceeding 1 is present in the string
def maxChange(str):
str = [i for i in str]
if (len(str) <= 1):
return 0
# Stores the count
minChanges = 0
# Iterate over the string
for i in range(len(str) - 1):
# Palindromic Subof Length 2
if (str[i] == str[i + 1]):
# Replace the next character
str[i + 1] = 'N'
# Increment changes
minChanges += 1
# Palindromic Subof Length 3
elif (i > 0 and str[i - 1] == str[i + 1]):
# Replace the next character
str[i + 1] = 'N'
# Increment changes
minChanges += 1
return minChanges
# Driver Code
if __name__ == '__main__':
str = "bbbbbbb"
print (maxChange(str))
# This code is contributed by mohit kumar 29.
C#
// C# Program to implement
// the above approach
using System;
public class GFG
{
// Function to count the changes required such
// that no palindromic subString of length
// exceeding 1 is present in the String
static int maxChange(char []str)
{
// Base Case
if (str.Length <= 1)
{
return 0;
}
// Stores the count
int minChanges = 0;
// Iterate over the String
for (int i = 0; i < str.Length - 1; i++)
{
// Palindromic SubString of Length 2
if (str[i] == str[i + 1])
{
// Replace the next character
str[i + 1] = 'N';
// Increment changes
minChanges += 1;
}
// Palindromic SubString of Length 3
else if (i > 0 && str[i - 1] == str[i + 1])
{
// Replace the next character
str[i + 1] = 'N';
// Increment changes
minChanges += 1;
}
}
return minChanges;
}
// Driver Code
public static void Main(String[] args)
{
String str = "bbbbbbb";
Console.Write(maxChange(str.ToCharArray()));
}
}
// This code contributed by shikhasingrajput
Javascript
输出:
4
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。