Break a Palindrome in C++



In this article, our task is to break a palindromic string. We have to replace exactly one character with any lowercase English letter such that the string becomes a lexicographically smallest possible string that isn't a palindrome. Now after doing so, we have to find the final string. If there is no way to do so, then return the empty string.

Example

Here is an example of breaking the given palindrome string by replacing one letter and the output is a non-palindromic lexicographically smallest string:

Input: abccba

Output: aaccba

In the above example, we can replace the first 'b' with any letter like: 'a', 'c', 'f', or 'z' to break the palindromic string. But, we have to break the string such that the string is lexicographically smallest. So, we have replaced 'b' with 'a'.

Lexicographical Smallest String

A lexicographical smallest string is a string that is sorted in dictionary-increasing order. For example: aab < abb < abc

Here are the two approaches for breaking the palindrome string into a lexicographically smallest string by replacing a single character:

Break a Palindrome Using Two Pointers

In this approach, we use two pointer variables: left and right that points to starting and the last index of the string respectively.

  • We iterate over the characters of string until the left pointer is less than the right pointer.
  • Then we check if the string has a single-character. We return an empty string if the string has a single character as a single character string can not be further broken.
  • Since the given string is a palindrome(i.e., the left and right characters will be the same), we check only the left side characters using s[left].
  • We replace the left side characters with 'a' for non 'a' characters and increase the left and decrease the right counter.
  • If all the left side characters are 'a', then change the last characters to 'b' and the string is then returned.

Example

In this example, we have used the above steps to break the palindrome string for lexicographically smallest string using two pointers:

#include <bits/stdc++.h>
using namespace std;

string breakPal(string s)
{
   if (s.size() == 1)
      return "";
   int left = 0, right = s.size() - 1;
   while (left < right)
   {
      if (s[left] != 'a')
      {
         s[left] = 'a';
         return s;
      }
      left++;
      right--;
   }
   s[s.size() - 1] = 'b';
   return s;
}

int main()
{
   char str[100] = "nayan";
   cout << "Given palindromic string is: " << str << endl;
   cout << "After breaking the palindrome: ";
   cout << breakPal(str) << endl;
}

The output of the above code is as follows:

Given palindromic string is: nayan
After breaking the palindrome: aayan

Break a Palindrome Using Greedy Approach

The following approach uses the Greedy approach where we select the string character on the basis of current available information.

  • First, we check if the string has a single character. If the string has a single character, we return an empty string as a single-character string can not be further broken.
  • Since the given string is palindrome(i.e., the left and right characters will be the same), we check only the half characters and replace the non 'a' characters with 'a'. For example: In 'abc', replace 'b' with 'a'.
  • If all the left side characters are 'a', then change the last characters to 'b' and the string is then returned.

Example

The following example implements the above steps of greedy approach to break the palindrome string for lexicographically smallest string:

#include <bits/stdc++.h>
using namespace std;

string breakPal(string s)
{
   if (s.size() == 1)
      return "";
   for (int i = 0; i < s.size() / 2; ++i)
   {
      if (s[i] != 'a')
      {
         s[i] = 'a';
         return s;
      }
   }
   s[s.size() - 1] = 'b';
   return s;
}
int main()
{
   char str[100] = "nayan";
   cout << "Given palindromic string is: " << str << endl;
   cout << "After breaking the palindrome: ";
   cout << breakPal(str) << endl;
}

The output of the above code is as follows:

Given palindromic string is: nayan
After breaking the palindrome: aayan

Complexity Comparison

Here is a comparison of the time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Using Two Pointers O(n) O(1)
Using Greedy Approach O(n) O(1)
Updated on: 2025-06-17T17:57:32+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements