Check given string is oddly palindrome or not
Last Updated :
20 May, 2021
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>
Time complexity: O(N), N is the length of the string.
Auxiliary Space: O(N/2)
Similar Reads
Check given string is oddly palindrome or not | Set 2 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,
11 min read
Check if a given string is Even-Odd Palindrome or not Given a string str, the task is to check if the given string is Even-Odd Palindrome or not. An Even-Odd Palindrome string is defined to be a string whose characters at even indices form a Palindrome while the characters at odd indices also form a Palindrome separately. Examples: Input: str="abzzab"
7 min read
Check if any anagram of a string is palindrome or not Given an anagram string S of length N, the task is to check whether it can be made palindrome or not. Examples: Input : S = geeksforgeeks Output: NoExplanation: There is no palindrome anagram of given string Input: S = geeksgeeksOutput: YesExplanation: There are palindrome anagrams of given string.
8 min read
Check if both halves of a string are Palindrome or not Given a string str, the task is to check whether the given string can be split into two halves, each of which is palindromic. If it is possible, print Yes. Otherwise, print No. Examples: Input: str = "naan" Output: No Explanation: Since both halves "na" and "an" are not palindrome. Input: momdad Out
5 min read
Queries to check if substring[L...R] is palindrome or not Given a string str and Q queries. Every query consists of two numbers L and R. The task is to print if the sub-string[L...R] is palindrome or not. Examples: Input: str = "abacccde", Q[][] = {{0, 2}, {1, 2}, {2, 4}, {3, 5}} Output: Yes No No Yes Input: str = "abaaab", Q[][] = {{0, 1}, {1, 5}} Output:
12 min read