Check given string is oddly palindrome or not
Given string str, the task is to check if characters at the odd indexes of str form a palindrome string or not. If not then print “No” else print “Yes”.
Examples:
Input: str = “osafdfgsg”, N = 9
Output: Yes
Explanation:
Odd indexed characters are = { s, f, f, s }
so it will make palindromic string, “sffs”.
Input: str = “addwfefwkll”, N = 11
Output: No
Explanation:
Odd indexed characters are = {d, w, e, w, l}
so it will not make palindrome string, “dwewl”
Naive Approach: The naive approach is to create a new string by appending odd indexed characters of the given string. Then, simply check if the string formed is palindromic or not. If the string is palindromic then print “Yes” else print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string str)
{
int l = 0;
int h = str.size() - 1;
while (h > l) {
if (str[l++] != str[h--]) {
return false ;
}
}
return true ;
}
string makeOddString(string str)
{
string odd = "" ;
for ( int i = 1; i < str.size();
i += 2) {
odd += str[i];
}
return odd;
}
void checkOddlyPalindrome(string str)
{
string odd = makeOddString(str);
if (isPalindrome(odd))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
string str = "ddwfefwde" ;
checkOddlyPalindrome(str);
return 0;
}
|
Java
class GFG{
public static boolean isPalindrome(String str)
{
int l = 0 ;
int h = str.length() - 1 ;
while (h > l)
{
if (str.charAt(l) != str.charAt(h))
{
return false ;
}
l++;
h--;
}
return true ;
}
public static String makeOddString(String str)
{
String odd = "" ;
for ( int i = 1 ; i < str.length(); i += 2 )
{
odd += str.charAt(i);
}
return odd;
}
public static void checkOddlyPalindrome(String str)
{
String odd = makeOddString(str);
if (isPalindrome(odd))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String []args)
{
String str = "ddwfefwde" ;
checkOddlyPalindrome(str);
}
}
|
Python3
def isPalindrome( str ):
l = 0 ;
h = len ( str ) - 1 ;
while (h > l):
if ( str [l] ! = str [h]):
return False ;
l + = 1
h - = 1
return True ;
def makeOddString( str ):
odd = "";
for i in range ( 1 , len ( str ), 2 ):
odd + = str [i];
return odd;
def checkOddlyPalindrome( str ):
odd = makeOddString( str );
if (isPalindrome(odd)):
print ( "Yes" )
else :
print ( "No" )
str = "ddwfefwde" ;
checkOddlyPalindrome( str );
|
C#
using System;
class GFG{
static bool isPalindrome( string str)
{
int l = 0;
int h = str.Length - 1;
while (h > l)
{
if (str[l] != str[h])
{
return false ;
}
l++;
h--;
}
return true ;
}
static string makeOddString( string str)
{
string odd = "" ;
for ( int i = 1; i < str.Length; i += 2)
{
odd += str[i];
}
return odd;
}
static void checkOddlyPalindrome( string str)
{
string odd = makeOddString(str);
if (isPalindrome(odd))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static void Main()
{
string str = "ddwfefwde" ;
checkOddlyPalindrome(str);
}
}
|
Javascript
<script>
function isPalindrome(str)
{
var l = 0;
var h = str.length - 1;
while (h > l) {
if (str[l++] != str[h--]) {
return false ;
}
}
return true ;
}
function makeOddString(str)
{
var odd = "" ;
for ( var i = 1; i < str.length;
i += 2) {
odd += str[i];
}
return odd;
}
function checkOddlyPalindrome(str)
{
var odd = makeOddString(str);
if (isPalindrome(odd))
document.write( "Yes" );
else
document.write( "No" );
}
var str = "ddwfefwde" ;
checkOddlyPalindrome(str);
</script>
|
Time complexity: O(N), N is the length of the string.
Auxiliary Space: O(N/2)
Last Updated :
20 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...