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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Longest Common Substring | DP-29
- Longest substring of vowels
- Longest substring with count of 1s more than 0s
- Longest Palindromic Substring | Set 2
- Longest Palindromic Substring | Set 1
- Length of the longest valid substring
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest substring that do not contain any palindrome
- Print the longest common substring
- Longest repeating and non-overlapping substring
- Longest substring such that no three consecutive characters are same
- Longest equal substring with cost less than K
- Length of the longest substring with equal 1s and 0s
- Longest substring having K distinct vowels
- Longest palindromic string possible after removal of a substring