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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to check if the string str// is palindromic or notbool 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 strstring 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 notvoid 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 Codeint main(){ // Given string string str = "ddwfefwde"; // Function Call checkOddlyPalindrome(str); return 0;} |
Java
// Java program for the above approachclass GFG{ // Function to check if the String str// is palindromic or notpublic 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 strpublic 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 notpublic 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 Codepublic 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 notdef 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 strdef 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 notdef checkOddlyPalindrome(str): # Make odd indexed string odd = makeOddString(str); # Check for Palindrome if (isPalindrome(odd)): print("Yes") else: print("No")# Driver code# Given stringstr = "ddwfefwde";# Function callcheckOddlyPalindrome(str);# This code is contributed by grand_master |
C#
// C# program for the above approachusing System;class GFG{ // Function to check if the String str// is palindromic or notstatic 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 strstatic 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 notstatic 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 codestatic 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 notfunction 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 strfunction 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 notfunction 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 stringvar str = "ddwfefwde";// Function CallcheckOddlyPalindrome(str);// This code is contributed by itsok.</script> |
Yes
Time complexity: O(N), N is the length of the string.
Auxiliary Space: O(N/2)


.png)
