# Longest Non-palindromic substring

Given a string of size n. The task is to find the length of the largest substring which is not palindrome.

Examples:

```Input : abba
Output : 3
Here maximum length non-palindromic substring is
'abb' which is of length '3'. There could be other
non-palindromic sub-strings also of length three
like 'bba' in this case.

Input : a
Output : 0
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to consider every substring and check if it is palindrome or not. Finally return length of the longest non-palindromic substring.

An efficient solution is based on below approach.

```Check for the case where all characters of
the string are same or not.
If yes, then answer will be '0'.
Else check whether the given string of size
'n' is palindrome or not.
If yes, then answer will be 'n-1'
Else answer will be 'n' ```

## C++

 `// C++ implementation to find maximum length ` `// substring which is not palindrome ` `#include ` `using` `namespace` `std; ` ` `  `// utility function to check whether ` `// a string is palindrome or not ` `bool` `isPalindrome(string str) ` `{ ` `    ``// Check for palindrome. ` `    ``int` `n = str.size(); ` `    ``for` `(``int` `i=0; i < n/2; i++) ` `        ``if` `(str.at(i) != str.at(n-i-1)) ` `            ``return` `false``; ` ` `  `    ``// palindrome string ` `    ``return` `true``; ` `} ` ` `  `// function to find maximum length ` `// substring which is not palindrome ` `int` `maxLengthNonPalinSubstring(string str) ` `{ ` `    ``int` `n = str.size(); ` `    ``char` `ch = str.at(0); ` ` `  `    ``// to check whether all characters ` `    ``// of the string are same or not ` `    ``int` `i = 1; ` `    ``for` `(i=1; i

## Java

 `//Java implementation to find maximum length ` `//substring which is not palindrome ` `public` `class` `GFG ` `{ ` `    ``// utility function to check whether ` `    ``// a string is palindrome or not ` `    ``static` `Boolean isPalindrome(String str) ` `    ``{ ` `        ``int` `n = str.length(); ` ` `  `        ``// Check for palindrome. ` `        ``for` `(``int` `i = ``0``; i < n/``2``; i++) ` `            ``if` `(str.charAt(i) != str.charAt(n-i-``1``)) ` `                ``return` `false``; ` ` `  `        ``// palindrome string ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// function to find maximum length ` `    ``// substring which is not palindrome ` `    ``static` `int` `maxLengthNonPalinSubstring(String str) ` `    ``{ ` `        ``int` `n = str.length(); ` `        ``char` `ch = str.charAt(``0``); ` ` `  `        ``// to check whether all characters ` `        ``// of the string are same or not ` `        ``int` `i = ``1``; ` `        ``for` `(i = ``1``; i < n; i++) ` `            ``if``(str.charAt(i) != ch) ` `                ``break``; ` ` `  `        ``// All characters are same, we can't ` `        ``// make a non-palindromic string. ` `        ``if` `(i == n) ` `            ``return` `0``; ` ` `  `        ``// If string is palindrome, we can make ` `        ``// it non-palindrome by removing any ` `        ``// corner character ` `        ``if` `(isPalindrome(str)) ` `            ``return` `n-``1``; ` ` `  `        ``// Complete string is not a palindrome. ` `        ``return` `n; ` `    ``} ` ` `  `    ``// Driver Program to test above function ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String str = ``"abba"``; ` `        ``System.out.println(``"Maximum Length = "` `             ``+ maxLengthNonPalinSubstring(str)); ` `    ``} ` `} ` `// This code is contributed by Sumit Ghosh `

## Python 3

 `# Python 3 implementation to find maximum  ` `# length substring which is not palindrome ` ` `  `# utility function to check whether ` `# a string is palindrome or not ` `def` `isPalindrome(``str``): ` `     `  `    ``# Check for palindrome. ` `    ``n ``=` `len``(``str``) ` `    ``for` `i ``in` `range``(n ``/``/` `2``): ` `        ``if` `(``str``[i] !``=` `str``[n ``-` `i ``-` `1``]): ` `            ``return` `False` ` `  `    ``# palindrome string ` `    ``return` `True` ` `  `# function to find maximum length ` `# substring which is not palindrome ` `def` `maxLengthNonPalinSubstring(``str``): ` `    ``n ``=` `len``(``str``) ` `    ``ch ``=` `str``[``0``] ` ` `  `    ``# to check whether all characters ` `    ``# of the string are same or not ` `    ``i ``=` `1` `    ``for` `i ``in` `range``(``1``, n): ` `        ``if` `(``str``[i] !``=` `ch): ` `            ``break` ` `  `    ``# All characters are same, we can't ` `    ``# make a non-palindromic string. ` `    ``if` `(i ``=``=` `n): ` `        ``return` `0` ` `  `    ``# If string is palindrome, we can make ` `    ``# it non-palindrome by removing any ` `    ``# corner character ` `    ``if` `(isPalindrome(``str``)): ` `        ``return` `n ``-` `1` ` `  `    ``# Complete string is not a palindrome. ` `    ``return` `n ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``str` `=` `"abba"` `    ``print``(``"Maximum length ="``,  ` `           ``maxLengthNonPalinSubstring(``str``)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# implementation to find maximum length ` `// substring which is not palindrome ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// utility function to check whether ` `    ``// a string is palindrome or not ` `    ``static` `bool` `isPalindrome(String str) ` `    ``{ ` `        ``int` `n = str.Length; ` ` `  `        ``// Check for palindrome. ` `        ``for` `(``int` `i = 0; i < n / 2; i++) ` `            ``if` `(str[i] != str[n - i - 1]) ` `                ``return` `false``; ` ` `  `        ``// palindrome string ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// function to find maximum length ` `    ``// substring which is not palindrome ` `    ``static` `int` `maxLengthNonPalinSubstring(String str) ` `    ``{ ` `        ``int` `n = str.Length; ` `        ``char` `ch = str; ` ` `  `        ``// to check whether all characters ` `        ``// of the string are same or not ` `        ``int` `i = 1; ` `        ``for` `(i = 1; i < n; i++) ` `            ``if``(str[i] != ch) ` `                ``break``; ` ` `  `        ``// All characters are same, we can't ` `        ``// make a non-palindromic string. ` `        ``if` `(i == n) ` `            ``return` `0; ` ` `  `        ``// If string is palindrome, we can  ` `        ``// make it non-palindrome by removing  ` `        ``// any corner character ` `        ``if` `(isPalindrome(str)) ` `            ``return` `n-1; ` ` `  `        ``// Complete string is not a palindrome. ` `        ``return` `n; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``String str = ``"abba"``; ` `        ``Console.Write(``"Maximum Length = "` `                      ``+ maxLengthNonPalinSubstring(str)); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal `

## PHP

 ` `

Output:

```Maximum length = 3
```

Time Complexity: O(n)

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.