Check if all the palindromic sub-strings are of odd length
Last Updated :
05 Dec, 2022
Given a string ‘s’ check if all of its palindromic sub-strings are of odd length or not. If yes then print “YES” or “NO” otherwise.
Examples:
Input: str = “geeksforgeeks”
Output: NO
Since, “ee” is a palindromic sub-string of even length.
Input: str = “madamimadam”
Output: YES
Brute Force Approach: Simply, iterate over each sub-string of ‘s’ and check if it is a palindrome. If it is a palindrome then it must of odd length.
Below is the implementation of the above approach:
C++
#include<bits//stdc++.h>
using namespace std;
bool checkPalindrome(string s)
{
for ( int i = 0; i < s.length(); i++)
{
if (s[i] != s[s.length() - i - 1])
return false ;
}
return true ;
}
bool CheckOdd(string s)
{
int n = s.length();
for ( int i = 0; i < n; i++)
{
string x = "" ;
for ( int j = i; j < n; j++)
{
x += s[j];
if (x.length() % 2 == 0 &&
checkPalindrome(x) == true )
return false ;
}
}
return true ;
}
int main()
{
string s = "geeksforgeeks" ;
if (CheckOdd(s))
cout<<( "YES" );
else
cout<<( "NO" );
}
|
Java
import java.util.*;
class GFG
{
static boolean checkPalindrome(String s)
{
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) != s.charAt(s.length() - i - 1 ))
return false ;
}
return true ;
}
static boolean CheckOdd(String s)
{
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
String x = "" ;
for ( int j = i; j < n; j++)
{
x += s.charAt(j);
if (x.length() % 2 == 0 &&
checkPalindrome(x) == true )
return false ;
}
}
return true ;
}
public static void main(String args[])
{
String s = "geeksforgeeks" ;
if (CheckOdd(s))
System.out.print( "YES" );
else
System.out.print( "NO" );
}
}
|
Python
def checkPalindrome(s):
for i in range ( len (s)):
if (s[i] ! = s[ len (s) - i - 1 ]):
return False
return True
def CheckOdd(s):
n = len (s)
for i in range (n):
x = ""
for j in range (i, n):
x + = s[j]
if ( len (x) % 2 = = 0
and checkPalindrome(x) = = True ):
return False
return True
s = "geeksforgeeks"
if (CheckOdd(s)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG {
static bool checkPalindrome(String s)
{
for ( int i = 0; i < s.Length; i++)
{
if (s[i] != s[(s.Length - i - 1)])
return false ;
}
return true ;
}
static bool CheckOdd(String s)
{
int n = s.Length;
for ( int i = 0; i < n; i++)
{
String x = "" ;
for ( int j = i; j < n; j++)
{
x += s[j];
if (x.Length % 2 == 0 &&
checkPalindrome(x) == true )
return false ;
}
}
return true ;
}
public static void Main()
{
String s = "geeksforgeeks" ;
if (CheckOdd(s))
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
PHP
<?php
function checkPalindrome( $s )
{
for ( $i = 0; $i < strlen ( $s ); $i ++)
{
if ( $s [ $i ] != $s [ strlen ( $s ) - $i - 1])
return false;
}
return true;
}
function CheckOdd( $s )
{
$n = strlen ( $s );
for ( $i = 0; $i < $n ; $i ++)
{
$x = "" ;
for ( $j = $i ; $j < $n ; $j ++)
{
$x = $x . $s [ $i ];
if ( strlen ( $x ) % 2 == 0 &&
checkPalindrome( $x ) == true)
return false;
}
}
return true;
}
$s = "geeksforgeeks" ;
if (CheckOdd( $s ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function checkPalindrome(s)
{
for (let i = 0; i < s.length; i++)
{
if (s[i] != s[s.length - i - 1])
return false ;
}
return true ;
}
function CheckOdd(s)
{
let n = s.length;
for (let i = 0; i < n; i++)
{
let x = "" ;
for (let j = i; j < n; j++)
{
x += s[j];
if (x.length % 2 == 0 &&
checkPalindrome(x) == true )
return false ;
}
}
return true ;
}
let s = "geeksforgeeks" ;
if (CheckOdd(s))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(n3), where n is the length of the given string.
Auxiliary Space: O(n), where n is the length of the given string.
Efficient Approach: To check if all palindromic substrings of s have odd lengths, we can search for an even length palindromic substring of it. We know that every even length palindrome has at least two consecutive characters that are identical (e.g. cxxa, ee). Therefore, we can check two consecutive characters at a time to see if they are the same. If so, then s has an even length palindromic substring and hence output will be NO, and if we find no even length substring the answer will be YES. We can complete this checking after one string traversal.
Below is the implementation of the above approach:
C++
#include<bits//stdc++.h>
using namespace std;
bool CheckEven(string s)
{
for ( int i = 1; i < s.size(); ++i) {
if (s[i] == s[i - 1]) {
return true ;
}
}
return false ;
}
int main()
{
string s = "geeksforgeeks" ;
if (CheckEven(s)== false )
cout<<( "YES" );
else
cout<<( "NO" );
}
|
Java
import java.util.*;
class GFG
{
static boolean checkPalindrome(String s)
{
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) != s.charAt(s.length() - i - 1 ))
return false ;
}
return true ;
}
static boolean CheckOdd(String s)
{
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
String x = "" ;
for ( int j = i; j < n; j++)
{
x += s.charAt(j);
if (x.length() % 2 == 0 &&
checkPalindrome(x) == true )
return false ;
}
}
return true ;
}
public static void main(String args[])
{
String s = "geeksforgeeks" ;
if (CheckOdd(s))
System.out.print( "YES" );
else
System.out.print( "NO" );
}
}
|
Python3
def checkPalindrome(s):
for i in range ( len (s)):
if (s[i] ! = s[ len (s) - i - 1 ]):
return False
return True
def CheckOdd(s):
n = len (s)
for i in range (n):
x = ""
for j in range (i, n):
x + = s[j]
if ( len (x) % 2 = = 0
and checkPalindrome(x) = = True ):
return False
return True
s = "geeksforgeeks"
if (CheckOdd(s)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG {
static bool checkPalindrome(String s)
{
for ( int i = 0; i < s.Length; i++)
{
if (s[i] != s[(s.Length - i - 1)])
return false ;
}
return true ;
}
static bool CheckOdd(String s)
{
int n = s.Length;
for ( int i = 0; i < n; i++)
{
String x = "" ;
for ( int j = i; j < n; j++)
{
x += s[j];
if (x.Length % 2 == 0 &&
checkPalindrome(x) == true )
return false ;
}
}
return true ;
}
public static void Main()
{
String s = "geeksforgeeks" ;
if (CheckOdd(s))
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
PHP
<?php
function checkPalindrome( $s )
{
for ( $i = 0; $i < strlen ( $s ); $i ++)
{
if ( $s [ $i ] != $s [ strlen ( $s ) - $i - 1])
return false;
}
return true;
}
function CheckOdd( $s )
{
$n = strlen ( $s );
for ( $i = 0; $i < $n ; $i ++)
{
$x = "" ;
for ( $j = $i ; $j < $n ; $j ++)
{
$x = $x . $s [ $i ];
if ( strlen ( $x ) % 2 == 0 &&
checkPalindrome( $x ) == true)
return false;
}
}
return true;
}
$s = "geeksforgeeks" ;
if (CheckOdd( $s ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function checkPalindrome(s)
{
for (let i = 0; i < s.length; i++)
{
if (s[i] != s[(s.length - i - 1)])
return false ;
}
return true ;
}
function CheckOdd(s)
{
let n = s.length;
for (let i = 0; i < n; i++)
{
let x = "" ;
for (let j = i; j < n; j++)
{
x += s[j];
if (x.length % 2 == 0 &&
checkPalindrome(x) == true )
return false ;
}
}
return true ;
}
let s = "geeksforgeeks" ;
if (CheckOdd(s))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...