Skip to content
Related Articles

Related Articles

Improve Article

Check given string is oddly palindrome or not

  • Last Updated : 20 May, 2021
Geek Week

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++] != str[h--]) {
            return false;
        }
    }
 
    // Return true if the string is
    // palindromic
    return true;
}
 
// Function to make string using odd
// indices of string str
string makeOddString(string str)
{
    string odd = "";
    for (int i = 1; i < str.size();
         i += 2) {
        odd += str[i];
    }
 
    return odd;
}
 
// Functions checks if characters at
// odd index of the string forms
// palindrome or not
void checkOddlyPalindrome(string str)
{
 
    // Make odd indexed string
    string odd = makeOddString(str);
 
    // Check for Palindrome
    if (isPalindrome(odd))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
 
// Driver Code
int main()
{
    // Given string
    string str = "ddwfefwde";
 
    // Function Call
    checkOddlyPalindrome(str);
    return 0;
}

Java




// Java program for the above approach
class GFG{
     
// Function to check if the String str
// is palindromic or not
public static boolean isPalindrome(String str)
{
     
    // Iterate the String str from left
    // and right pointers
    int l = 0;
    int h = str.length() - 1;
 
    // Keep comparing characters
    // while they are same
    while (h > l)
    {
         
        // If they are not same
        // then return false
        if (str.charAt(l) != str.charAt(h))
        {
            return false;
        }
         
        l++;
        h--;
    }
 
    // Return true if the String is
    // palindromic
    return true;
}
 
// Function to make String using odd
// indices of String str
public static String makeOddString(String str)
{
    String odd = "";
     
    for(int i = 1; i < str.length(); i += 2)
    {
       odd += str.charAt(i);
    }
    return odd;
}
 
// Functions checks if characters at
// odd index of the String forms
// palindrome or not
public static void checkOddlyPalindrome(String str)
{
 
    // Make odd indexed String
    String odd = makeOddString(str);
 
    // Check for Palindrome
    if (isPalindrome(odd))
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// Driver Code
public static void main(String []args)
{
     
    // Given String
    String str = "ddwfefwde";
 
    // Function Call
    checkOddlyPalindrome(str);
}
}
 
// This code is contributed by grand_master

Python3




# Python3 program for the above approach
 
# Function to check if the string 
# str is palindromic or not
def isPalindrome(str):
 
    # Iterate the string str from
    # left and right pointers
    l = 0;
    h = len(str) - 1;
 
    # Keep comparing characters
    # while they are same
    while (h > l):
 
        # If they are not same
        # then return false
        if (str[l] != str[h]):
            return False;
             
        l += 1
        h -= 1
         
    # Return true if the string is
    # palindromic
    return True;
 
# Function to make string using odd
# indices of string str
def makeOddString(str):
 
    odd = "";
    for i in range(1, len(str), 2):
        odd += str[i];
     
    return odd;
 
# Functions checks if characters at
# odd index of the string forms
# palindrome or not
def checkOddlyPalindrome(str):
 
    # Make odd indexed string
    odd = makeOddString(str);
 
    # Check for Palindrome
    if (isPalindrome(odd)):
        print("Yes")
    else:
        print("No")
 
# Driver code
 
# Given string
str = "ddwfefwde";
 
# Function call
checkOddlyPalindrome(str);
 
# This code is contributed by grand_master

C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if the String str
// is palindromic or not
static bool isPalindrome(string str)
{
     
    // Iterate the String str from left
    // and right pointers
    int l = 0;
    int h = str.Length - 1;
 
    // Keep comparing characters
    // while they are same
    while (h > l)
    {
         
        // If they are not same
        // then return false
        if (str[l] != str[h])
        {
            return false;
        }
         
        l++;
        h--;
    }
 
    // Return true if the String is
    // palindromic
    return true;
}
 
// Function to make String using odd
// indices of String str
static string makeOddString(string str)
{
    string odd = "";
     
    for(int i = 1; i < str.Length; i += 2)
    {
        odd += str[i];
    }
    return odd;
}
 
// Functions checks if characters at
// odd index of the String forms
// palindrome or not
static void checkOddlyPalindrome(string str)
{
 
    // Make odd indexed String
    string odd = makeOddString(str);
 
    // Check for Palindrome
    if (isPalindrome(odd))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver code
static void Main()
{
     
    // Given String
    string str = "ddwfefwde";
     
    // Function Call
    checkOddlyPalindrome(str);
}
}
 
// This code is contributed by divyeshrabadiya07

Javascript




<script>
 
// Javascript program for the above approach
 
// Function to check if the string str
// is palindromic or not
function isPalindrome(str)
{
 
    // Iterate the string str from left
    // and right pointers
    var l = 0;
    var h = str.length - 1;
 
    // Keep comparing characters
    // while they are same
    while (h > l) {
 
        // If they are not same
        // then return false
        if (str[l++] != str[h--]) {
            return false;
        }
    }
 
    // Return true if the string is
    // palindromic
    return true;
}
 
// Function to make string using odd
// indices of string str
function makeOddString(str)
{
    var odd = "";
    for (var i = 1; i < str.length;
         i += 2) {
        odd += str[i];
    }
 
    return odd;
}
 
// Functions checks if characters at
// odd index of the string forms
// palindrome or not
function checkOddlyPalindrome(str)
{
 
    // Make odd indexed string
    var odd = makeOddString(str);
 
    // Check for Palindrome
    if (isPalindrome(odd))
        document.write( "Yes" );
    else
        document.write( "No" );
}
 
// Driver Code
 
// Given string
var str = "ddwfefwde";
 
// Function Call
checkOddlyPalindrome(str);
 
// This code is contributed by itsok.
</script>
Output:
Yes

Time complexity: O(N), N is the length of the string. 
Auxiliary Space: O(N/2)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :