Open In App

Check given string is oddly palindrome or not

Last Updated : 20 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to check if the string str
// is palindromic or not
bool isPalindrome(string str)
{

    // Iterate the string str from left
    // and right pointers
    int l = 0;
    int h = str.size() - 1;

    // Keep comparing characters
    // while they are same
    while (h > l) {

        // If they are not same
        // then return false
        if (str[l++]