Given a string of lowercase, find the length of the longest substring that does not contain any palindrome as a substring.
Examples:
Input : str = "daiict" Output : 3 dai, ict are longest substring that do not contain any palindrome as substring Input : str = "a" Output : 0 a is itself a palindrome
The idea is to observe that if any character forms a palindrome, it can not be included in any substring. So, in that case, the required substring will be picked from before or after that character which forms a palindrome.Â
Therefore, a simple solution is to traverse the string and, for each character, check whether it forms a palindrome of length 2 or 3 with its adjacent characters. If it does not, then increase the length of the substring, otherwise re-initialize the length of the substring to zero. Using this approach, find the length of the maximum substring.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach Â
#include <bits/stdc++.h> using namespace std; Â
// Function to find the length of the longest // substring int lenoflongestnonpalindrome(string s) {     // initializing the variables     int max1 = 1, len = 0; Â
    for ( int i = 0; i < s.length() - 1; i++) {         // checking palindrome of size 2         // example: aa         if (s[i] == s[i + 1])             len = 0;         // checking palindrome of size 3         // example: aba         else if (s[i + 1] == s[i - 1] && i > 0)             len = 1;         else // incrementing length of substring             len++;         max1 = max(max1, len + 1); // finding maximum     } Â
    // if there exists single character then     // it is always palindrome     if (max1 == 1)         return 0;     else         return max1; } Â
// Driver Code int main() { Â Â Â Â string s = "synapse" ; Â Â Â Â cout << lenoflongestnonpalindrome(s) << "\n" ; Â Â Â Â return 0; } |
Java
// Java implementation of the above approach import java.util.Arrays; import java.lang.Math; Â
class GFG { Â
    // Function to find the length of the longest     // substring     public static int lenoflongestnonpalindrome(String s)     {         // initializing the variables         int max1 = 1 , len = 0 ;         char [] new_str = s.toCharArray(); Â
        for ( int i = 0 ; i < new_str.length - 1 ; i++) {             // checking palindrome of size 2             // example: aa             if (new_str[i] == new_str[i + 1 ])                 len = 0 ;             // checking palindrome of size 3             // example: aba             else if (i > 0 && (new_str[i + 1 ] == new_str[i - 1 ]))                 len = 1 ;             else // incrementing length of substring                 len++;             max1 = Math.max(max1, len + 1 ); // finding maximum         } Â
        // if there exits single character then         // it is always palindrome         if (max1 == 1 )             return 0 ;         else             return max1;     } Â
    // Driver Code     public static void main(String[] args)     {         String s = "synapse" ;         System.out.println(lenoflongestnonpalindrome(s));     } } Â
// This code is contributed by princiraj1992 |
Python3
# Python3 implementation of the above approach Â
# Function to find the length # of the longest substring def lenoflongestnonpalindrome(s): Â
    # initializing the variables     max1, length = 1 , 0 Â
    for i in range ( 0 , len (s) - 1 ):                  # checking palindrome of         # size 2 example: aa         if s[i] = = s[i + 1 ]:             length = 0                      # checking palindrome of         # size 3 example: aba         elif s[i + 1 ] = = s[i - 1 ] and i > 0 :             length = 1         else : # incrementing length of substring             length + = 1         max1 = max (max1, length + 1 ) # finding maximum Â
    # If there exits single character     # then it is always palindrome     if max1 = = 1 :         return 0     else :         return max1 Â
# Driver Code if __name__ = = "__main__" : Â
    s = "synapse"     print (lenoflongestnonpalindrome(s))      # This code is contributed by Rituraj Jain |
C#
// C# implementation of the above approach using System; Â Â Â Â Â class GFG { Â
    // Function to find the length of the longest     // substring     public static int lenoflongestnonpalindrome(String s)     {         // initializing the variables         int max1 = 1, len = 0;         char [] new_str = s.ToCharArray(); Â
        for ( int i = 0; i < new_str.Length - 1; i++)         {             // checking palindrome of size 2             // example: aa             if (new_str[i] == new_str[i + 1])                 len = 0;                              // checking palindrome of size 3             // example: aba             else if (i > 0 && (new_str[i + 1] == new_str[i - 1]))                 len = 1;             else // incrementing length of substring                 len++;             max1 = Math.Max(max1, len + 1); // finding maximum         } Â
        // if there exits single character then         // it is always palindrome         if (max1 == 1)             return 0;         else             return max1;     } Â
    // Driver Code     public static void Main(String[] args)     {         String s = "synapse" ;         Console.WriteLine(lenoflongestnonpalindrome(s));     } } Â
// This code has been contributed by 29AjayKumar |
PHP
<?php // PHP implementation of the above approach Â
// Function to find the length of the longest // substring function lenoflongestnonpalindrome( $s ) {     // initializing the variables     $max1 = 1; $len = 0; Â
    for ( $i = 0; $i < strlen ( $s ) - 1; $i ++)     {         // checking palindrome of size 2         // example: aa         if ( $s [ $i ] == $s [ $i + 1])             $len = 0;                      // checking palindrome of size 3         // example: aba         else if ( $s [ $i + 1] == $s [ $i - 1] && $i > 0)             $len = 1;         else // incrementing length of substring             $len ++;         $max1 = max( $max1 , $len + 1); // finding maximum     } Â
    // if there exits single character then     // it is always palindrome     if ( $max1 == 1)         return 0;     else         return $max1 ; } Â
// Driver Code $s = "synapse" ; echo lenoflongestnonpalindrome( $s ), "\n" ; Â
// This code is contributed by AnkitRai01 Â
?> |
Javascript
<script> Â
// JavaScript implementation of the above approach Â
// Function to find the length of the longest // substring function lenoflongestnonpalindrome(s) {     // initializing the variables     let max1 = 1, len = 0; Â
    for (let i = 0; i < s.length - 1; i++) {         // checking palindrome of size 2         // example: aa         if (s[i] == s[i + 1])             len = 0;         // checking palindrome of size 3         // example: aba         else if (s[i + 1] == s[i - 1] && i > 0)             len = 1;         else // incrementing length of substring             len++;         max1 = Math.max(max1, len + 1); // finding maximum     } Â
    // if there exits single character then     // it is always palindrome     if (max1 == 1)         return 0;     else         return max1; } Â
// Driver Code Â
    let s = "synapse" ;     document.write(lenoflongestnonpalindrome(s) + "<br>" );      Â
//This code is contributed by Manoj </script> |
7
Â
Time complexity: O(n) where n is length of input string
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!