Length of the longest substring that do not contain any palindrome
Last Updated :
18 Oct, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int lenoflongestnonpalindrome(string s)
{
int max1 = 1, len = 0;
for ( int i = 0; i < s.length() - 1; i++) {
if (s[i] == s[i + 1])
len = 0;
else if (s[i + 1] == s[i - 1] && i > 0)
len = 1;
else
len++;
max1 = max(max1, len + 1);
}
if (max1 == 1)
return 0;
else
return max1;
}
int main()
{
string s = "synapse" ;
cout << lenoflongestnonpalindrome(s) << "\n" ;
return 0;
}
|
Java
import java.util.Arrays;
import java.lang.Math;
class GFG {
public static int lenoflongestnonpalindrome(String s)
{
int max1 = 1 , len = 0 ;
char [] new_str = s.toCharArray();
for ( int i = 0 ; i < new_str.length - 1 ; i++) {
if (new_str[i] == new_str[i + 1 ])
len = 0 ;
else if (i > 0 && (new_str[i + 1 ] == new_str[i - 1 ]))
len = 1 ;
else
len++;
max1 = Math.max(max1, len + 1 );
}
if (max1 == 1 )
return 0 ;
else
return max1;
}
public static void main(String[] args)
{
String s = "synapse" ;
System.out.println(lenoflongestnonpalindrome(s));
}
}
|
Python3
def lenoflongestnonpalindrome(s):
max1, length = 1 , 0
for i in range ( 0 , len (s) - 1 ):
if s[i] = = s[i + 1 ]:
length = 0
elif s[i + 1 ] = = s[i - 1 ] and i > 0 :
length = 1
else :
length + = 1
max1 = max (max1, length + 1 )
if max1 = = 1 :
return 0
else :
return max1
if __name__ = = "__main__" :
s = "synapse"
print (lenoflongestnonpalindrome(s))
|
C#
using System;
class GFG
{
public static int lenoflongestnonpalindrome(String s)
{
int max1 = 1, len = 0;
char [] new_str = s.ToCharArray();
for ( int i = 0; i < new_str.Length - 1; i++)
{
if (new_str[i] == new_str[i + 1])
len = 0;
else if (i > 0 && (new_str[i + 1] == new_str[i - 1]))
len = 1;
else
len++;
max1 = Math.Max(max1, len + 1);
}
if (max1 == 1)
return 0;
else
return max1;
}
public static void Main(String[] args)
{
String s = "synapse" ;
Console.WriteLine(lenoflongestnonpalindrome(s));
}
}
|
PHP
<?php
function lenoflongestnonpalindrome( $s )
{
$max1 = 1; $len = 0;
for ( $i = 0; $i < strlen ( $s ) - 1; $i ++)
{
if ( $s [ $i ] == $s [ $i + 1])
$len = 0;
else if ( $s [ $i + 1] == $s [ $i - 1] && $i > 0)
$len = 1;
else
$len ++;
$max1 = max( $max1 , $len + 1);
}
if ( $max1 == 1)
return 0;
else
return $max1 ;
}
$s = "synapse" ;
echo lenoflongestnonpalindrome( $s ), "\n" ;
?>
|
Javascript
<script>
function lenoflongestnonpalindrome(s)
{
let max1 = 1, len = 0;
for (let i = 0; i < s.length - 1; i++) {
if (s[i] == s[i + 1])
len = 0;
else if (s[i + 1] == s[i - 1] && i > 0)
len = 1;
else
len++;
max1 = Math.max(max1, len + 1);
}
if (max1 == 1)
return 0;
else
return max1;
}
let s = "synapse" ;
document.write(lenoflongestnonpalindrome(s) + "<br>" );
</script>
|
Time complexity: O(n) where n is length of input string
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...