Length of longest substring that do not contain any palindrome

In C++, we have predefined function max() that will be used for finding any longest substring that does contain any palindrome. A palindrome string is a group of characters that remains the same even after reversing.

Let’s take an example of a palindrome string to make the longest non-palindrome substring.

  • The string malayalam itself is a palindrome but we need to identify the longest non-palindrome substring. When we change the string malayalam( length=9 ) to alayalam then we get the longest non-palindrome substring length i.e, 8.

  • The string synapse is a non-palindrome string and its length is 7.


The following syntax is used in the program −

substr( size_a position, size_a length ) 


  • size_a − Thie integer type.

  • position −- The first character of the position is to be copied.

  • Length − The length of the substring.

var_name = max( var_name, size_a length) 


  • var_name − The name of the variable.

  • size_a − The integer type.

  • length − The length of the substring.


  • We will create a global function named ‘isPalindrome’ and pass the string 's' as a parameter to that function.

  • In the global function ‘isPalindrome’ we are calculating the length of string ‘s’ as input and storing it in a variable ‘n’ and using for loop the string character ‘s’ iterate from the first to the middle character.

  • Then we will create an if-statement to check whether both ‘(i)-th’ and ‘(num-i-1)-th’ characters are equivalent or not and return false if it is a palindrome otherwise return true.

  • In the main function we will define the variable ‘str’ and store the input string in it. Then find the length of ‘str’ and store it in another variable named ‘n’.

  • We are storing 0 in an integer variable ‘answer’ as it will be used for finding the length of the non-palindrome substring.

  • Start two nested for loops to iterate all possible substrings of str.

  • Use an if statement under the second for loop to call the above function named ‘isPalindrome’ for each substring.

  • We are finding the length of longest non-palindrome substring with the help of predefined function max(relate step 5).

  • Finally, the for nested loop counter will check if the substring is a non-palindrome or palindrome and then print the length of the longest substring of non-palindrome string.


In this example, we follow the above algorithm's entire approach to learn how to find the length of the longest substring that does not contain a palindrome.

using namespace std;
bool isPalindrome(string s) {
   int n = s.length();
   for (int i = 0; i < n/2; i++) {
      if (s[i] != s[n-i-1]) {
         return false;
   return true;
int main() {
   string str = "madam"; 
   // “madam” string is itself a palindrome but its longest substring check so that it doesn’t become a palindrome and give its length in the output.
   int n = str.length();
   int answer = 0;
   for (int i = 0; i < n; i++) {
      for (int j = i+1; j <= n; j++) {
         if (!isPalindrome(str.substr(i, j-i))) {
            answer = max(answer, j-i);
            // return the maximum length of non-palindrome substring.
   cout << "The length of longest substring that does not contain any palindrome: "<<answer << endl;
   return 0;


The length of longest substring that does not contain any palindrome: 4


We have explored the concept of finding the length of the longest substring that does not contain any palindrome. Our analysis has observed that this problem can be approached by breaking it down into smaller sub-problems. This is also a real-life problem that has practical applications in fields such as natural language processing and bioinformatics.

Updated on: 20-Apr-2023


Kickstart Your Career

Get certified by completing the course

Get Started