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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Length of the longest substring without repeating characters
- Longest Palindromic Substring | Set 1
- Longest Palindromic Substring | Set 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Longest Even Length Substring such that Sum of First and Second Half is same
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Suffix Tree Application 6 - Longest Palindromic Substring
- Longest repeating and non-overlapping substring
- Find length of longest subsequence of one string which is substring of another string
- Longest common substring in binary representation of two numbers
- Longest substring of vowels
- Length of the longest substring with equal 1s and 0s
- SequenceMatcher in Python for Longest Common Substring
- Longest Common Substring (Space optimized DP solution)