给定一个由小写字符组成的字符串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的回文串。因此,通过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
输出:
4
时间复杂度: O(N)
辅助空间: O(1)