Check if suffix and prefix of a string are palindromes
Last Updated :
05 Dec, 2022
Given a string ‘s’, the task is to check whether the string has both prefix and suffix substrings of length greater than 1 which are palindromes.
Print ‘YES’ if the above condition is satisfied or ‘NO’ otherwise.
Examples:
Input : s = abartbb
Output : YES
Explanation : The string has prefix substring 'aba'
and suffix substring 'bb' which are both palindromes, so the output is 'YES'.
Input : s = abcc
Output : NO
Explanation : The string has no prefix substring which is palindrome,
it only has a suffix substring 'cc' which is a palindrome.
So the output is 'NO'.
Approach:
- First, check all the prefix substrings of length > 1 to find if there are any which is a palindrome.
- Check all the suffix substrings as well.
- If both the conditions are true, then the output is ‘YES’.
- Otherwise, the output is ‘NO’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string r)
{
string p = r;
reverse(p.begin(), p.end());
return (r == p);
}
bool CheckStr(string s)
{
int l = s.length();
int i;
for (i = 2; i <= l; i++) {
if (isPalindrome(s.substr(0, i)))
break ;
}
if (i == (l+1))
return false ;
i = 2;
for (i = 2; i <= l; i++) {
if (isPalindrome(s.substr(l-i, i)))
return true ;
}
return false ;
}
int main()
{
string s = "abccbarfgdbd" ;
if (CheckStr(s))
cout << "YES\n" ;
else
cout << "NO\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String reverse(String input)
{
char [] a = input.toCharArray();
int l, r = 0 ;
r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
static boolean isPalindrome(String r)
{
String p = r;
p = reverse(p);
return (r.equals(p));
}
static boolean CheckStr(String s)
{
int l = s.length();
int i;
for (i = 2 ; i <= l; i++)
{
if (isPalindrome(s.substring( 0 , i)))
{
break ;
}
}
if (i == (l + 1 ))
{
return false ;
}
i = 2 ;
for (i = 2 ; i <=l; i++)
{
if (isPalindrome(s.substring(l-i,l)))
{
return true ;
}
}
return false ;
}
public static void main(String args[])
{
String s = "abccbarfgdbd" ;
if (CheckStr(s))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def isPalindrome(r):
p = r[:: - 1 ]
return r = = p
def CheckStr(s):
l = len (s)
i = 0
for i in range ( 2 , l + 1 ):
if isPalindrome(s[ 0 :i]) = = True :
break
if i = = (l + 1 ):
return False
for i in range ( 2 , l + 1 ):
if isPalindrome(s[l - i : l]) = = True :
return True
return False
if __name__ = = "__main__" :
s = "abccbarfgdbd"
if CheckStr(s) = = True :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static String reverse(String input)
{
char [] a = input.ToCharArray();
int l, r = 0;
r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" ,a);
}
static Boolean isPalindrome(String r)
{
String p = r;
p = reverse(p);
return (r.Equals(p));
}
static Boolean CheckStr(String s)
{
int l = s.Length;
int i;
for (i = 2; i <= l; i++)
{
if (isPalindrome(s.Substring(0, i)))
{
break ;
}
}
if (i == (l + 1))
{
return false ;
}
i = 2;
for (i = 2; i <=l; i++)
{
if (isPalindrome(s.Substring(l-i,i)))
{
return true ;
}
}
return false ;
}
public static void Main(String []args)
{
String s = "abccbarfgdbd" ;
if (CheckStr(s))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
function isPalindrome( $r )
{
$p = $r ;
strrev ( $p );
return ( $r == $p );
}
function CheckStr( $s )
{
$l = strlen ( $s );
for ( $i = 2; $i <= $l ; $i ++)
{
if (isPalindrome( substr ( $s , 0, $i )))
break ;
}
if ( $i == ( $l + 1))
return false;
$i = 2;
for ( $i = 2; $i <= $l ; $i ++)
{
if (isPalindrome( substr ( $s , $l -
$i , $i )))
return true;
}
return false;
}
$s = "abccbarfgdbd" ;
if (CheckStr( $s ))
echo ( "YES\n" );
else
echo ( "NO\n" );
?>
|
Javascript
<script>
function isPalindrome(r)
{
var p = r;
p.split( '' ).reverse().join( '' );
return (r == p);
}
function CheckStr( s)
{
var l = s.length;
var i;
for (i = 2; i <= l; i++) {
if (isPalindrome(s.substring(0, i)))
break ;
}
if (i == (l+1))
return false ;
i = 2;
for (i = 2; i <= l; i++) {
if (isPalindrome(s.substring(l-i, l)))
return true ;
}
return false ;
}
var s = "abccbarfgdbd" ;
if (CheckStr(s))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(n^2), where n is the length of the given string.
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...