Given a string of size n. The task is to find the length of the largest substring which is not palindrome.
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
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'
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 email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Longest substring with count of 1s more than 0s
- Longest substring of vowels
- Longest Common Substring | DP-29
- Longest Palindromic Substring | Set 2
- Longest Palindromic Substring | Set 1
- Length of the longest substring with equal 1s and 0s
- Print the longest common substring
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest valid substring
- Longest substring having K distinct vowels
- Length of the longest substring that do not contain any palindrome
- Longest repeating and non-overlapping substring
- Print Longest substring without repeating characters
- Longest substring of 0s in a string formed by k concatenations
- Length of the longest substring with no consecutive same letters