Check if string can be rearranged so that every Odd length Substring is Palindrome
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.
Examples:
Input: S = “oiooi”
Output: YES
The string can be rearranged as “oioio”
Input: S = “yuyuo”
Output: NO
Approach:
- 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:
C++
// C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std; // Function to check is it // possible to rearrange the string // such that every odd length // substring is palindrome bool IsPossible(string s) { // Length of the string int n = s.length(); // To count number of distinct // character in string set<char> count; // To count frequency of // each character map<char, int> map; for (int i = 0; i < n; i++) { // Inserting into set count.insert(s[i]); // Incrementing the frequency map[s[i]] += 1; } // All characters in // the string are same if (count.size() == 1) { return true; } // There are more than 2 different // character in string if (count.size() > 2) { return false; } // Currently there is 2 different // character in string auto it = count.begin(); // Get the frequencies of the // characters that present // in string int x = 0, y = 0; x = map[*it]; it++; y = map[*it]; // Difference between their // count is less than or // equal to 1 if (abs(x - y) <= 1) { return true; } return false; } // Driver code int main() { string s = "aaaddad"; if (IsPossible(s)) cout << "YES\n"; else cout << "NO\n"; return 0; } |
chevron_right
filter_none
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to check is it // possible to rearrange the string // such that every odd length // substring is palindrome static boolean IsPossible(String s) { // Length of the string int n = s.length(); // To count number of distinct // character in string HashSet<Character> count = new HashSet<>(); // To count frequency of // each character HashMap<Character, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { // Inserting into set count.add(s.charAt(i)); // Incrementing the frequency map.put(s.charAt(i), map.get(s.charAt(i)) == null ? 1 : map.get(s.charAt(i)) + 1); } // All characters in // the string are same if (count.size() == 1) return true; // There are more than 2 different // character in string if (count.size() > 2) return false; String newString = count.toArray().toString(); // Currently there is 2 different // character in string int j = 0; char it = newString.charAt(j); // Get the frequencies of the // characters that present // in string int x = 0, y = 0; x = map.get(it) == null ? 0 : map.get(it); j++; it = newString.charAt(j); y = map.get(it) == null ? 0 : map.get(it); // Difference between their // count is less than or // equal to 1 if (Math.abs(x - y) <= 1) return true; return false; } // Driver Code public static void main(String[] args) { String s = "aaaddad"; if (IsPossible(s)) System.out.println("YES"); else System.out.println("NO"); } } // This code is contributed by // sanjeev2552 |
chevron_right
filter_none
Python3
# Python3 implementation of # the above approach # Function to check is it # possible to rearrange the string # such that every odd length # substring is palindrome def IsPossible(s) : # Length of the string n = len(s); # To count number of distinct # character in string count = set(); # To count frequency of # each character map = dict.fromkeys(s, 0); for i in range(n) : # Inserting into set count.add(s[i]); # Incrementing the frequency map[s[i]] += 1; # All characters in # the string are same if (len(count) == 1) : return True; # There are more than 2 different # character in string if (len(count) > 2) : return False; # Currently there is 2 different # character in string j = 0 it = list(count)[j]; # Get the frequencies of the # characters that present # in string x = 0; y = 0; x = map[it]; j += 1 it = list(count)[j]; y = map[it]; # Difference between their # count is less than or # equal to 1 if (abs(x - y) <= 1) : return True; return False; # Driver code if __name__ == "__main__" : s = "aaaddad"; if (IsPossible(s)) : print("YES"); else : print("NO"); # This code is contributed by AnkitRai01 |
chevron_right
filter_none
Output:
YES
Recommended Posts:
- 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
- Length of the longest substring that do not contain any palindrome
- Queries to check if substring[L...R] is palindrome or not
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Find length of longest subsequence of one string which is substring of another string
- Convert to a string that is repetition of a substring of k length
- Maximum length substring with highest frequency in a string
- Binary String of given length that without a palindrome of size 3
- Maximum even length sub-string that is permutation of a palindrome
- Queries to check if string B exists as substring in string A
- Check if a string is substring of another
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Python | Check if a Substring is Present in a Given String
- Check if a given string is a rotation of a 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.

