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.
Input: str = “naan”
Since both halves “na” and “an” are not palindrome.
Since both half “mom” and “dad” are palindromes.
Follow the steps below to solve the problem:
- Iterate over the first ((N / 2) / 2 – 1) indices of the string.
- Simultaneously check if both the halves are palindrome or not by the following two conditions:
- If S[i] is not equal to S[N/2 – 1 – i], then first half is not palindromic.
- If S[N/2 + i] is not equal to S[N – 1 – i], then second half is not palindromic.
- If none of the above condition is satidfied even once during the iteration, then both halves are palindromic. Print Yes.
- Otherwise, print No.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Check if both halves of the string have same set of characters
- Check if both halves of the string have at least one different character
- Check if both halves of the string have same set of characters in Python
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Python program to check if a string is palindrome or not
- Check if there exists any sub-sequence in a string which is not palindrome
- Check given string is oddly palindrome or not
- Check if a given string is Even-Odd Palindrome or not
- Check given string is oddly palindrome or not | Set 2
- Check if any anagram of a string is palindrome or not
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Maximum cost of splitting given Binary Tree into two halves
- Interchanging first and second halves of stings
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Check linked list with a loop is palindrome or not
- Check if a doubly linked list of characters is palindrome or not
- To check a number is palindrome or not without using any extra space
- Check if number is palindrome or not in Octal
- Recursive program to check if number is palindrome or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.