Given a string S. The task is to check whether it is possible to rearrange the string such that every substring of odd length is a palindrome.
Input: S = “oiooi”
The string can be rearranged as “oioio”
Input: S = “yuyuo”
- The very first observation is if all the characters of the string are same then every substring of odd length is a palindrome and we do not need to rearrange them.
- Second observation is if the number of distinct characters are more than 2 then it is impossible to rearrange.
- Now if the number of distinct characters are exactly 2 then to get all odd length substring to be a palindrome, the difference of their count must be less than or equal to 1, and if this satisfy then we rearrange the string in alternate manner means for i <— ( 1 to n – 1 ). Where n is the length of the string.
Below is the implementation of above approach:
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.
- Longest substring whose characters can be rearranged to form a Palindrome
- Check if characters of a given string can be rearranged to form a palindrome
- Check if a string can be rearranged to form special palindrome
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Length of the longest substring that do not contain any palindrome
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Check if a given string is Even-Odd Palindrome or not
- Minimum K such that every substring of length atleast K contains a character c
- Total number of odd length palindrome sub-sequence around each centre
- Find max length odd parity substring
- Queries to check if substring[L...R] is palindrome or not
- Check if string can be made lexicographically smaller by reversing any substring
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Find length of longest subsequence of one string which is substring of another string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.