We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Prerequisite: For those that don't understand what permutations mean(me) this is essential to being able to solve this problem. You cannot assume that n! will give you the number of permutations for a string with duplicate objects. The reason for this is that n! assumes that all the objects are distinct, which is not the case in "aba". When some of the objects are identical, we need to adjust the formula for permutations to account for the fact that some of the arrangements will be identical. Check out this video for a better understanding: https://www.youtube.com/watch?v=juGgfHsO-xM&ab_channel=MITOpenCourseWare
The way to solve this problem is as follows: Given some substring and the ability to choose which characters to put in our maximal palindrome from that substring, we can form the maximum palindrome by greedily taking all even count characters. This will always result in a valid palindrom, test it yourself. We continue to be greedy and take all the even amounts of characters from all the odd character counts, this inevitably leaves behind a single character from each odd character count. Now suppose we have built the maximal palindrome, how would be get the number of permutations of it? We would simply use the permutation equation that keeps note of duplicate objects(characters in our case). We then need to multiply by the number of different middle characters we could place in our palindrome resulting in the total number of maximal palindromes in that substring, our answer.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Palindromes
You are viewing a single comment's thread. Return to all comments →
Prerequisite: For those that don't understand what permutations mean(me) this is essential to being able to solve this problem. You cannot assume that n! will give you the number of permutations for a string with duplicate objects. The reason for this is that n! assumes that all the objects are distinct, which is not the case in "aba". When some of the objects are identical, we need to adjust the formula for permutations to account for the fact that some of the arrangements will be identical. Check out this video for a better understanding: https://www.youtube.com/watch?v=juGgfHsO-xM&ab_channel=MITOpenCourseWare
The way to solve this problem is as follows: Given some substring and the ability to choose which characters to put in our maximal palindrome from that substring, we can form the maximum palindrome by greedily taking all even count characters. This will always result in a valid palindrom, test it yourself. We continue to be greedy and take all the even amounts of characters from all the odd character counts, this inevitably leaves behind a single character from each odd character count. Now suppose we have built the maximal palindrome, how would be get the number of permutations of it? We would simply use the permutation equation that keeps note of duplicate objects(characters in our case). We then need to multiply by the number of different middle characters we could place in our palindrome resulting in the total number of maximal palindromes in that substring, our answer.