Longest Non-palindromic substring
Last Updated :
11 Jul, 2022
Given a string of size n. The task is to find the length of the largest substring which is not a palindrome.
Examples:
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 the length of the longest non-palindromic substring.
An efficient solution is based on the 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'
Implementation:
C++
// C++ implementation to find maximum length
// substring which is not palindrome
#include <bits/stdc++.h>
using namespace std;
// utility function to check whether
// a string is palindrome or not
bool isPalindrome(string str)
{
// Check for palindrome.
int n = str.size();
for (int i=0; i < n/2; i++)
if (str.at(i) != str.at(n-i-1))
return false;
// palindrome string
return true;
}
// function to find maximum length
// substring which is not palindrome
int maxLengthNonPalinSubstring(string str)
{
int n = str.size();
char ch = str.at(0);
// to check whether all characters
// of the string are same or not
int i = 1;
for (i=1; i<n; i++)
if (str.at(i) != ch)
break;
// All characters are same, we can't
// make a non-palindromic string.
if (i == n)
return 0;
// If string is palindrome, we can make
// it non-palindrome by removing any
// corner character
if (isPalindrome(str))
return n-1;
// Complete string is not a palindrome.
return n;
}
// Driver program to test above
int main()
{
string str = "abba";
cout << "Maximum length = "
<< maxLengthNonPalinSubstring(str);
return 0;
}
Java
//Java implementation to find maximum length
//substring which is not palindrome
public class GFG
{
// utility function to check whether
// a string is palindrome or not
static Boolean isPalindrome(String str)
{
int n = str.length();
// Check for palindrome.
for (int i = 0; i < n/2; i++)
if (str.charAt(i) != str.charAt(n-i-1))
return false;
// palindrome string
return true;
}
// function to find maximum length
// substring which is not palindrome
static int maxLengthNonPalinSubstring(String str)
{
int n = str.length();
char ch = str.charAt(0);
// to check whether all characters
// of the string are same or not
int i = 1;
for (i = 1; i < n; i++)
if(str.charAt(i) != ch)
break;
// All characters are same, we can't
// make a non-palindromic string.
if (i == n)
return 0;
// If string is palindrome, we can make
// it non-palindrome by removing any
// corner character
if (isPalindrome(str))
return n-1;
// Complete string is not a palindrome.
return n;
}
// Driver Program to test above function
public static void main(String args[])
{
String str = "abba";
System.out.println("Maximum Length = "
+ maxLengthNonPalinSubstring(str));
}
}
// This code is contributed by Sumit Ghosh
Python 3
# Python 3 implementation to find maximum
# length substring which is not palindrome
# utility function to check whether
# a string is palindrome or not
def isPalindrome(str):
# Check for palindrome.
n = len(str)
for i in range(n // 2):
if (str[i] != str[n - i - 1]):
return False
# palindrome string
return True
# function to find maximum length
# substring which is not palindrome
def maxLengthNonPalinSubstring(str):
n = len(str)
ch = str[0]
# to check whether all characters
# of the string are same or not
i = 1
for i in range(1, n):
if (str[i] != ch):
break
# All characters are same, we can't
# make a non-palindromic string.
if (i == n):
return 0
# If string is palindrome, we can make
# it non-palindrome by removing any
# corner character
if (isPalindrome(str)):
return n - 1
# Complete string is not a palindrome.
return n
# Driver Code
if __name__ == "__main__":
str = "abba"
print("Maximum length =",
maxLengthNonPalinSubstring(str))
# This code is contributed by ita_c
C#
// C# implementation to find maximum length
// substring which is not palindrome
using System;
class GFG {
// utility function to check whether
// a string is palindrome or not
static bool isPalindrome(String str)
{
int n = str.Length;
// Check for palindrome.
for (int i = 0; i < n / 2; i++)
if (str[i] != str[n - i - 1])
return false;
// palindrome string
return true;
}
// function to find maximum length
// substring which is not palindrome
static int maxLengthNonPalinSubstring(String str)
{
int n = str.Length;
char ch = str[0];
// to check whether all characters
// of the string are same or not
int i = 1;
for (i = 1; i < n; i++)
if(str[i] != ch)
break;
// All characters are same, we can't
// make a non-palindromic string.
if (i == n)
return 0;
// If string is palindrome, we can
// make it non-palindrome by removing
// any corner character
if (isPalindrome(str))
return n-1;
// Complete string is not a palindrome.
return n;
}
// Driver code
public static void Main()
{
String str = "abba";
Console.Write("Maximum Length = "
+ maxLengthNonPalinSubstring(str));
}
}
// This code is contributed by nitin mittal
PHP
<?php
// PHP implementation to find maximum length
// substring which is not palindrome
// utility function to check whether
// a string is palindrome or not
function isPalindrome($str)
{
$n = strlen($str);
// Check for palindrome.
for ($i = 0; $i < $n / 2; $i++)
if ($str[$i] != $str[$n - $i - 1])
return false;
// palindrome string
return true;
}
// function to find maximum length
// substring which is not palindrome
function maxLengthNonPalinSubstring($str)
{
$n = strlen($str);
$ch = $str[0];
// to check whether all characters
// of the string are same or not
$i = 1;
for ($i = 1; $i < $n; $i++)
if($str[$i] != $ch)
break;
// All characters are same, we can't
// make a non-palindromic string.
if ($i == $n)
return 0;
// If string is palindrome, we can
// make it non-palindrome by removing
// any corner character
if (isPalindrome($str))
return ($n - 1);
// Complete string is not a palindrome.
return $n;
}
// Driver code
$str = "abba";
echo "Maximum Length = ",
maxLengthNonPalinSubstring($str);
// This code is contributed by ajit
?>
JavaScript
<script>
//Javascript implementation to find maximum length
//substring which is not palindrome
// utility function to check whether
// a string is palindrome or not
function isPalindrome(str)
{
let n=str.length;
// Check for palindrome.
for (let i = 0; i < n/2; i++)
{
if(str[i] != str[n-i-1])
return false;
}
// palindrome string
return true;
}
// function to find maximum length
// substring which is not palindrome
function maxLengthNonPalinSubstring(str)
{
let n = str.length;
let ch=str[0];
// to check whether all characters
// of the string are same or not
let i = 1;
for (i = 1; i < n; i++)
{
if(str[i] != ch)
{
break;
}
}
// All characters are same, we can't
// make a non-palindromic string.
if (i == n)
return 0;
// If string is palindrome, we can make
// it non-palindrome by removing any
// corner character
if (isPalindrome(str))
return n-1;
// Complete string is not a palindrome.
return n;
}
// Driver Program to test above function
let str = "abba";
document.write("Maximum Length = " + maxLengthNonPalinSubstring(str));
// This code is contributed by avanitrachhadiya2155
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Palindrome String Coding Problems A string is called a palindrome if the reverse of the string is the same as the original one.Example: âmadamâ, âracecarâ, â12321â.Palindrome StringProperties of a Palindrome String:A palindrome string has some properties which are mentioned below:A palindrome string has a symmetric structure which m
2 min read
Palindrome String Given a string s, the task is to check if it is palindrome or not.Example:Input: s = "abba"Output: 1Explanation: s is a palindromeInput: s = "abc" Output: 0Explanation: s is not a palindromeUsing Two-Pointers - O(n) time and O(1) spaceThe idea is to keep two pointers, one at the beginning (left) and
13 min read
Check Palindrome by Different Language
Easy Problems on Palindrome
Sentence Palindrome Given a sentence s, the task is to check if it is a palindrome sentence or not. A palindrome sentence is a sequence of characters, such as a word, phrase, or series of symbols, that reads the same backward as forward after converting all uppercase letters to lowercase and removing all non-alphanumer
9 min read
Check if actual binary representation of a number is palindrome Given a non-negative integer n. The problem is to check if binary representation of n is palindrome or not. Note that the actual binary representation of the number is being considered for palindrome checking, no leading 0âs are being considered. Examples : Input : 9 Output : Yes (9)10 = (1001)2 Inp
6 min read
Print longest palindrome word in a sentence Given a string str, the task is to print longest palindrome word present in the string str.Examples: Input : Madam Arora teaches Malayalam Output: Malayalam Explanation: The string contains three palindrome words (i.e., Madam, Arora, Malayalam) but the length of Malayalam is greater than the other t
14 min read
Count palindrome words in a sentence Given a string str and the task is to count palindrome words present in the string str. Examples: Input : Madam Arora teaches malayalam Output : 3 The string contains three palindrome words (i.e., Madam, Arora, malayalam) so the count is three. Input : Nitin speaks malayalam Output : 2 The string co
5 min read
Check if characters of a given string can be rearranged to form a palindrome Given a string, Check if the characters of the given string can be rearranged to form a palindrome. For example characters of "geeksogeeks" can be rearranged to form a palindrome "geeksoskeeg", but characters of "geeksforgeeks" cannot be rearranged to form a palindrome. Recommended PracticeAnagram P
14 min read
Lexicographically first palindromic string Rearrange the characters of the given string to form a lexicographically first palindromic string. If no such string exists display message "no palindromic string". Examples: Input : malayalam Output : aalmymlaa Input : apple Output : no palindromic string Simple Approach: 1. Sort the string charact
13 min read