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 firstname.lastname@example.org. 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.
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.
- Longest substring of vowels
- Longest Common Substring | DP-29
- Longest Palindromic Substring | Set 2
- Longest Palindromic Substring | Set 1
- Longest substring with count of 1s more than 0s
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest valid substring
- Longest equal substring with cost less than K
- Print the longest common substring
- Longest repeating and non-overlapping substring
- Longest substring having K distinct vowels
- Longest substring such that no three consecutive characters are same
- Length of the longest substring that do not contain any palindrome
- Length of the longest substring with equal 1s and 0s
- Print Longest substring without repeating characters
- Longest substring of vowels with no two adjacent alphabets same
- Longest substring of only 4's from the first N characters of the infinite string
- Length of the longest substring without repeating characters
- SequenceMatcher in Python for Longest Common Substring
- Length of the longest substring with no consecutive same letters