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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if string formed // by odd indices is palindromic or not bool isOddStringPalindrome( string str, int n) { int oddStringSize = n / 2; // Check if length of OddString // odd, to consider edge case bool lengthOdd = ((oddStringSize % 2 == 1) ? true : false); stack<char> s; int i = 1; int c = 0; // Push odd index character of // first half of str in stack while (i < n && c < oddStringSize / 2) { s.push(str[i]); i += 2; c++; } // Middle element of odd length // palindromic string is not // compared if (lengthOdd) i = i + 2; while (i < n && s.size() > 0) { if (s.top() == str[i]) s.pop(); else break; i = i + 2; } // If stack is empty // then return true if (s.size() == 0) return true; return false; } // Driver Code int main() { int N = 10; // Given string string s = "aeafacafae"; if (isOddStringPalindrome(s, N)) cout << "Yes\n"; else cout << "No\n"; return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to check if String formed // by odd indices is palindromic or not static boolean isOddStringPalindrome(String str, int n) { int oddStringSize = n / 2; // Check if length of OddString // odd, to consider edge case boolean lengthOdd = ((oddStringSize % 2 == 1) ? true : false); Stack<Character> s = new Stack<Character>(); int i = 1; int c = 0; // Push odd index character of // first half of str in stack while (i < n && c < oddStringSize / 2) { s.add(str.charAt(i)); i += 2; c++; } // Middle element of odd length // palindromic String is not // compared 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 stack is empty // then return true if (s.size() == 0) return true; return false; } // Driver Code public static void main(String[] args) { int N = 10; // Given String String s = "aeafacafae"; if (isOddStringPalindrome(s, N)) System.out.print("Yes\n"); else System.out.print("No\n"); } } // This code is contributed by Rohit_ranjan |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to check if String formed // by odd indices is palindromic or not static bool isOddStringPalindrome(String str, int n) { int oddStringSize = n / 2; // Check if length of OddString // odd, to consider edge case bool lengthOdd = ((oddStringSize % 2 == 1) ? true : false); Stack<char> s = new Stack<char>(); int i = 1; int c = 0; // Push odd index character of // first half of str in stack while (i < n && c < oddStringSize / 2) { s.Push(str[i]); i += 2; c++; } // Middle element of odd length // palindromic String is not // compared if (lengthOdd) i = i + 2; while (i < n && s.Count > 0) { if (s.Peek() == str[i]) s.Pop(); else break; i = i + 2; } // If stack is empty // then return true if (s.Count == 0) return true; return false; } // Driver Code public static void Main(String[] args) { int N = 10; // Given String String s = "aeafacafae"; if (isOddStringPalindrome(s, N)) Console.Write("Yes\n"); else Console.Write("No\n"); } } // This code is contributed by Princi Singh |
Yes
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 intialize 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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Functions checks if characters at // odd index of the string forms // palindrome or not bool isOddStringPalindrome(string str, int n) { // Initialise two pointers int left, right; if (n % 2 == 0) { left = 1; right = n - 1; } else { left = 1; right = n - 2; } // Iterate till left <= right while (left < n && right >= 0 && left < right) { // If there is a mismatch occurs // then return false if (str[left] != str[right]) return false; // Increment and decrement the left // and right pointer by 2 left += 2; right -= 2; } return true; } // Driver Code int main() { int n = 10; // Given String string s = "aeafacafae"; // Function Call if (isOddStringPalindrome(s, n)) cout << "Yes\n"; else cout << "No\n"; return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Functions checks if characters at // odd index of the String forms // palindrome or not static boolean isOddStringPalindrome(String str, int n) { // Initialise two pointers int left, right; if (n % 2 == 0) { left = 1; right = n - 1; } else { left = 1; right = n - 2; } // Iterate till left <= right while (left < n && right >= 0 && left < right) { // If there is a mismatch occurs // then return false if (str.charAt(left) != str.charAt(right)) return false; // Increment and decrement the left // and right pointer by 2 left += 2; right -= 2; } return true; } // Driver Code public static void main(String[] args) { int n = 10; // Given String String s = "aeafacafae"; // Function Call if (isOddStringPalindrome(s, n)) System.out.print("Yes\n"); else System.out.print("No\n"); } } // This code is contributed by Rohit_ranjan |
C#
// C# program for the above approach using System; class GFG{ // Functions checks if characters at // odd index of the String forms // palindrome or not static bool isOddStringPalindrome(String str, int n) { // Initialise two pointers int left, right; if (n % 2 == 0) { left = 1; right = n - 1; } else { left = 1; right = n - 2; } // Iterate till left <= right while (left < n && right >= 0 && left < right) { // If there is a mismatch occurs // then return false if (str[left] != str[right]) return false; // Increment and decrement the left // and right pointer by 2 left += 2; right -= 2; } return true; } // Driver Code public static void Main(String[] args) { int n = 10; // Given String String s = "aeafacafae"; // Function Call if (isOddStringPalindrome(s, n)) Console.Write("Yes\n"); else Console.Write("No\n"); } } // This code is contributed by Rohit_ranjan |
Yes
Time Complexity: O(N)
Space Complexity: O(1)
Recommended Posts:
- Check given string is oddly palindrome or not
- Check if a given string is Even-Odd Palindrome or not
- Check if both halves of a string are Palindrome or not
- Check whether the given string is Palindrome using Stack
- Check if a given string is a rotation of a palindrome
- Check if a string is palindrome in C using pointers
- Check if it is possible to create a palindrome string from given N
- Check if any anagram of a string is palindrome or not
- C Program to Check if a Given String is Palindrome
- Java program to check whether a string is a Palindrome
- Given two strings check which string makes a palindrome first
- Check if there exists any sub-sequence in a string which is not palindrome
- Python program to check if a string is palindrome or not
- Recursive function to check if a string is palindrome
- Check if characters of a given string can be rearranged to form a palindrome
- TCP Client-Server Program to Check if a Given String is Palindrome
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Check if string is palindrome after removing all consecutive duplicates
- Python program to check if given string is vowel Palindrome
- Check if a string can be rearranged to form special palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


