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: Please refer the Set 1 of this article for naive approach
Efficient Approach: The idea is to check if oddString formed by appending odd indices of given string forms a palindrome or not. So, instead of creating oddString and then checking for the palindrome, we can use a stack to optimize running time. Below are the steps:
- For checking oddString to be a palindrome, we need to compare odd indices characters of the first half of the string with odd characters of the second half of string.
- Push odd index character of the first half of the string- into a stack.
- To compare odd index character of the second half with the first half do the following:
- Pop characters from the stack and match it with the next odd index character of the second half of string.
- If the above two characters are not equal then string formed by odd indices is not palindromic. Print “NO” and break from the loop.
- Else match for every remaining odd character of the second half of the string.
- In the end, we need to check if the size of the stack is zero. If it is, then string formed by odd indices will be palindrome, else not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isOddStringPalindrome(
string str, int n)
{
int oddStringSize = n / 2;
bool lengthOdd
= ((oddStringSize % 2 == 1)
? true
: false );
stack< char > s;
int i = 1;
int c = 0;
while (i < n
&& c < oddStringSize / 2) {
s.push(str[i]);
i += 2;
c++;
}
if (lengthOdd)
i = i + 2;
while (i < n && s.size() > 0) {
if (s.top() == str[i])
s.pop();
else
break ;
i = i + 2;
}
if (s.size() == 0)
return true ;
return false ;
}
int main()
{
int N = 10;
string s = "aeafacafae" ;
if (isOddStringPalindrome(s, N))
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isOddStringPalindrome(String str,
int n)
{
int oddStringSize = n / 2 ;
boolean lengthOdd = ((oddStringSize % 2 == 1 ) ?
true : false );
Stack<Character> s = new Stack<Character>();
int i = 1 ;
int c = 0 ;
while (i < n && c < oddStringSize / 2 )
{
s.add(str.charAt(i));
i += 2 ;
c++;
}
if (lengthOdd)
i = i + 2 ;
while (i < n && s.size() > 0 )
{
if (s.peek() == str.charAt(i))
s.pop();
else
break ;
i = i + 2 ;
}
if (s.size() == 0 )
return true ;
return false ;
}
public static void main(String[] args)
{
int N = 10 ;
String s = "aeafacafae" ;
if (isOddStringPalindrome(s, N))
System.out.print( "Yes\n" );
else
System.out.print( "No\n" );
}
}
|
Python3
def isOddStringPalindrome( str , n):
oddStringSize = n / / 2 ;
lengthOdd = True if (oddStringSize % 2 = = 1 ) else False
s = []
i = 1
c = 0
while (i < n and c < oddStringSize / / 2 ):
s.append( str [i])
i + = 2
c + = 1
if (lengthOdd):
i = i + 2
while (i < n and len (s) > 0 ):
if (s[ len (s) - 1 ] = = str [i]):
s.pop()
else :
break
i = i + 2
if ( len (s) = = 0 ):
return True
return False ;
if __name__ = = "__main__" :
N = 10
s = "aeafacafae"
if (isOddStringPalindrome(s, N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool isOddStringPalindrome(String str,
int n)
{
int oddStringSize = n / 2;
bool lengthOdd = ((oddStringSize % 2 == 1) ?
true : false );
Stack< char > s = new Stack< char >();
int i = 1;
int c = 0;
while (i < n && c < oddStringSize / 2)
{
s.Push(str[i]);
i += 2;
c++;
}
if (lengthOdd)
i = i + 2;
while (i < n && s.Count > 0)
{
if (s.Peek() == str[i])
s.Pop();
else
break ;
i = i + 2;
}
if (s.Count == 0)
return true ;
return false ;
}
public static void Main(String[] args)
{
int N = 10;
String s = "aeafacafae" ;
if (isOddStringPalindrome(s, N))
Console.Write( "Yes\n" );
else
Console.Write( "No\n" );
}
}
|
Javascript
<script>
function isOddStringPalindrome( str, n)
{
var oddStringSize = parseInt(n / 2);
var lengthOdd
= ((oddStringSize % 2 == 1)
? true
: false );
var s = [];
var i = 1;
var c = 0;
while (i < n
&& c < parseInt(oddStringSize / 2)) {
s.push(str[i]);
i += 2;
c++;
}
if (lengthOdd)
i = i + 2;
while (i < n && s.length > 0) {
if (s[s.length-1] == str[i])
s.pop();
else
break ;
i = i + 2;
}
if (s.length == 0)
return true ;
return false ;
}
var N = 10;
var s = "aeafacafae" ;
if (isOddStringPalindrome(s, N))
document.write( "Yes" );
else
document.write( "No\n" );
</script>
|
Time Complexity: O(N)
Space Complexity: O(N)
Efficient Approach 2: The above naive approach can be solved without using extra space. We will use Two Pointers Technique, left and right pointing to first odd index from the beginning and from the end respectively. Below are the steps:
- Check whether length of string is odd or even.
- If length is odd, then initialize left = 1 and right = N-2
- Else, initialize left = 1 and right = N-1
- Increment left pointer by 2 position and decrement right pointer by 2 position.
- Keep comparing the characters pointed by pointers till left <= right.
- If no mis-match occurs then, the string is palindrome, else it is not palindromic.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isOddStringPalindrome(string str,
int n)
{
int left, right;
if (n % 2 == 0) {
left = 1;
right = n - 1;
}
else {
left = 1;
right = n - 2;
}
while (left < n && right >= 0
&& left < right) {
if (str[left] != str[right])
return false ;
left += 2;
right -= 2;
}
return true ;
}
int main()
{
int n = 10;
string s = "aeafacafae" ;
if (isOddStringPalindrome(s, n))
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isOddStringPalindrome(String str,
int n)
{
int left, right;
if (n % 2 == 0 )
{
left = 1 ;
right = n - 1 ;
}
else
{
left = 1 ;
right = n - 2 ;
}
while (left < n && right >= 0 &&
left < right)
{
if (str.charAt(left) !=
str.charAt(right))
return false ;
left += 2 ;
right -= 2 ;
}
return true ;
}
public static void main(String[] args)
{
int n = 10 ;
String s = "aeafacafae" ;
if (isOddStringPalindrome(s, n))
System.out.print( "Yes\n" );
else
System.out.print( "No\n" );
}
}
|
Python3
def isOddStringPalindrome( Str , n):
left, right = 0 , 0
if (n % 2 = = 0 ):
left = 1
right = n - 1
else :
left = 1
right = n - 2
while (left < n and right > = 0 and
left < right):
if ( Str [left] ! = Str [right]):
return False
left + = 2
right - = 2
return True
if __name__ = = '__main__' :
n = 10
Str = "aeafacafae"
if (isOddStringPalindrome( Str , n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isOddStringPalindrome(String str,
int n)
{
int left, right;
if (n % 2 == 0)
{
left = 1;
right = n - 1;
}
else
{
left = 1;
right = n - 2;
}
while (left < n && right >= 0 &&
left < right)
{
if (str[left] !=
str[right])
return false ;
left += 2;
right -= 2;
}
return true ;
}
public static void Main(String[] args)
{
int n = 10;
String s = "aeafacafae" ;
if (isOddStringPalindrome(s, n))
Console.Write( "Yes\n" );
else
Console.Write( "No\n" );
}
}
|
Javascript
<script>
function isOddStringPalindrome(str, n)
{
var left, right;
if (n % 2 == 0) {
left = 1;
right = n - 1;
}
else {
left = 1;
right = n - 2;
}
while (left < n && right >= 0
&& left < right) {
if (str[left] != str[right])
return false ;
left += 2;
right -= 2;
}
return true ;
}
var n = 10;
var s = "aeafacafae" ;
if (isOddStringPalindrome(s, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
29 May, 2021
Like Article
Save Article