Minimum Characters to be Replaced to make a String Concatenation of a K-length Palindromic String

Tracking down the most modest number of characters that should be changed to transform a given string into a link of K-length palindromic substrings is a common issue in the field of string control. A string that peruses similar advances and in reverse is known as a palindromic string. Examples are "radar" or "level." The fundamental ideas, methods, and potential optimization strategies that can be used to effectively address this issue will all be covered in this article. By the conclusion, readers will be able to handle similar string manipulation issues since they will have a comprehensive understanding of the steps required.

The problem will be explained in full in the following paragraphs, and then each approach's pros and cons will be discussed. The chosen approaches will be thoroughly examined, with code samples being provided to show how they might be used. We will also examine each approach's temporal complexity to learn more about how effective it is given various input quantities.

Methods Used

  • Brute-Force Approach

  • Sliding Window Approach

Brute-Force Approach

The Brute-Force The approach for finding the fewest characters to be supplanted to form a string concatenation of a K-length palindromic string includes checking all possible substrings of length K within the given string. It takes after the steps: set two pointers, cleared out and right, to the begin and conclusion of the K-character substring, initialize a variable to track the least substitutions, and iterate over the string, upgrading the window with the proper pointer moving one step right each time. For each window, check in case it could be a palindrome by comparing characters from left and right, and tally the number of substitutions required on the off chance that it's not a palindrome. Keep track of the fewest replacements found so far. Proceed with this preparation until the conclusion of the string. The result will be the fewest substitutions required to realize the specified K-length palindromic substring. In any case, this approach has high time complexity, making it wasteful for huge strings.


  • Consider each substring of length K as you iterate through the provided string.

  • Verify whether or not each substring is a palindrome.

  • Count how many characters would need to be changed if it weren't already a palindrome.

  • Maintain the substring with the fewest replacements possible.

  • Make a palindrome by changing the characters in the minimal replacement substring.


#include <iostream>
#include <string>
using namespace std;

string minimalReplacementPalindromeSubstring(const string& str, int K) {
   int n = str.length();
   string minReplacementSubstr;

   for (int i = 0; i <= n - K; i++) {
      string substr = str.substr(i, K);
      int replacements = 0;
      for (int j = 0; j < K / 2; j++) {
         if (substr[j] != substr[K - 1 - j]) {

      if (replacements < minReplacementSubstr.length() || minReplacementSubstr.empty()) {
         minReplacementSubstr = substr;

   return minReplacementSubstr;

int main() {
   string input = "iurefhuhfrati";
   int K = 4;

   string result = minimalReplacementPalindromeSubstring(input, K);
   cout << "Minimal Replacement Substring: " << result << endl;

   return 0;


Minimal Replacement Substring: rati

Sliding Window Approach

The Sliding Window Approach may be a method utilized to effectively solve problems, including subarray or substring operations. Within the setting of finding the least characters to be supplanted to create a string concatenation of a K-length palindromic string, this approach includes keeping up a fixed-size window (substring) of K characters while navigating the input string.

The calculation sets up two pointers, 'left' and 'right,' at first indicating the start and conclusion of the K-character substring. At that point, it decides the number of substitutions required to convert this substring into a palindrome. To keep track of the fewest substitutions required, a variable 'min_replacements' is initialized.


  • Set up two pointers, left and right, to the start and end of the principal K-character substring.

  • Decide the number of replacements that are expected to transform the substring into a palindrome.

  • To keep track of the bare minimum replacements required, initialize the variable min_replacements.

  • Update the window by moving the right pointer to the right by one place.

  • Move the right pointer if the current window is a palindrome.

  • Calculate the amount of replacements required and, if necessary, change min_replacements if the current window is not a palindrome.

  • To update the window, move the left pointer one space to the right.

  • Up to the string's conclusion, repeat steps 4 through 7.

  • The substring's characters should be replaced with the fewest possible replacements.


#include <iostream>
#include <string>
using namespace std;

int minReplacementsForPalindrome(string s, int k) {
   int left = 0, right = k - 1, min_replacements = 0;
   while (right < s.length()) {
      int i = left, j = right;
      while (i < j) {
         if (s[i] != s[j])
   return min_replacements;

int main() {
   string input_string = "abcxyzuvw"; // Replace this with your desired input string
   int k = 3; // Replace this with the desired value of K
   int result = minReplacementsForPalindrome(input_string, k);
   cout << "Minimum replacements required: " << result << endl;
   return 0;


Minimum replacements required: 7


This article examines the issue of finding the least number of characters required to convert a given string into a concatenation of K-length palindromic substrings. It investigates two fundamental approaches to fathoming this issue: the Brute-Force Approach and the Sliding Window Approach. The Brute-Force Approach includes checking all conceivable substrings of length K inside the given string, deciding on the off chance that they are palindromes, and checking the vital substitutions. Be that as it may, this approach has a high level of complexity, making it wasteful for huge strings.

On the other hand, the Sliding Window Approach optimizes the method by keeping up a fixed-size window and proficiently overhauling it while navigating the input string. The article gives code tests and experiences into the pros and cons of each strategy to help users get it and fathom comparative string control issues more successfully.

Updated on: 04-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started