Number of possible palindrome anagrams for a given word - algorithm

I have to find No. of palindrome anagrams are possible for a given word.
Suppose the word is aaabbbb.My approach is
Prepare a hash map that contains no. of time each letter is appearing
For my example it will be
a--->3
b--->4
If length of string is even then no. of occurrence of each letter should be even to form palindrome of given word else no of
palindrome anagrams is 0
If length of string is odd then at max one occurrence of letter can be odd and other should be even.
This two above steps was for finding that weather a given word can can form palindrome or not.
Now for finding no of palindrome anagrams, what approach should I follow?

First thing to notice is that if the word is an odd length, then there must be exactly one character with an odd number of occurrences. If the word is an even length, then there must be no characters with an odd number of occurrences. In either case, you're looking for how many ways you can arrange the pairs of characters. You're looking for the number of permutations since order matters:
n = number of character pairs (aaaabbb would have 3 pairs, aabbcccc would have 4 pairs)
(n)!/( number_of_a_pairs! * number_of_b_pairs! * etc..)
So in the aaaabbb case, you're finding the permutations of aab:
3!/2!1! = 3
baa = baabaab
aba = abababa
aab = aabbbaa
And in the aabbcccc case, you're finding the permutations of abcc:
4!/2! = 12:
abcc
acbc
accb
bacc
bcac
bcca
cabc
cacb
cbac
cbca
ccab
ccba

Related

Given a collection of strings, can we make all the strings equal

The allowed operations are removal of character from a string and adding that character to another string. We can repeat the operation as many times as we want.
Given list = ['CAA', 'CBB'].
We can remove 'A' from the first string and add it to the second string.
'CA', 'CBBA'.
Now, we can remove 'B" from the second string and add in the middle of string 'CA'.
So, we have 'CBA' and 'CBA'
Step-1. Compute the frequency of each character in all the strings of the list, let's say it as stringList.
Step-2: Compute the length of stringList and let's call it as length. (length is equal to the number of strings in the list.)
Step-3: Now, for the frequency of each character, Check if it is divisible by length. If any frequency is not divisible by length then it's not possible to equate the strings.
If it is possible:
Just distribute the characters equally among the strings to get an answer list.

Unique Permutations - with exceptions

I just did a quiz and it had a question that I brute forced... but I'm certain there is a "mathmatical" formula that "solves" it.
Permutations of a String
Say you have a string "abcdef"... count all unique permutations of the string. For a string with all unique options, this is simple: Length Factoral. So 6! = 720 unique combinations.
Unique Permutations
Now, when you add duplicates... you take the factoral, and devide by the product of the unique letters: "aaabbb" => 6! / (3! * 3!) => 720 / 36 => 20 unique combinations.
Unique Permutations, with exclusions
The part that stumps me:
You have a string, possibly with duplicate data... except now, exclude permutations that start with a space (or a dot, for visibility):
"aa.bb" => "aabb." is a valid permutation... ".aabb" is not.
"aa.bb.cc" => "aa..bbcc" valid permutation. ".aabbcc." not valid. "..aabbcc" is not valid
"a.." => has one valid permutation: "a.."... the others are all duplicates or start with spaces.
My Solution
My solution - brute force - was to create the permutations... and manually exclude those starting with spaces... O(N!) if I remember correctly.
I know it has something to do with factorals and the number of spaces. But the final answer eludes me.
I should be able to take the length, divide by the counts... and the calculate the distinct number that start with spaces and subtract that.
You partition the first character as a separate case: there are fewer choices for that character. This changes the first factor of the numerator of the calculation. For instance, aa.bb.cc has only 6 choices for the first character, not 8. Therefore, the calculation that was
8! / (2! 2! 2! 2!) -- four duplicates
is now
(6 * 7!) / (2! 2! 2! 2!) -- we still have four duplicates
Let's say that you have 5 unique letters,
so you'll have 5! combinations.
Now when you have 5 unique alphabets and one ., then
In the first position, you'll put one of those 5 alphabets. Then, you'll put the rest of them (4 alphabets and 1 .) in 5! ways,
The result being 5*5!
So, the answer according to me should be along the lines of
Let's say you have x unique alphabets, y alphabets in all and z spaces
So the answer should be
y * (y+z-1)! / (diving for repeated alphabets and spaces combinations)

Pair up strings to form palindromes

Given N strings each of at max 1000 length. We can concatenate pair of strings by ends. Like if one is "abc" and other is "cba" then we can get "abccba" as well as "cbaabc". Some string may be left without concatenation to any other string. Also no string can be concatenated to itself.
We can only concatenate those two strings that form a palindrome. So I need to tell the minimum number of strings left after making such pairs.
Example : Let we have 9 strings :
aabbaabb
bbaabbaa
aa
bb
a
bbaa
bba
bab
ab
Then here answer is 5
Explanation : Here are 5 strings :
"aabbaabb" + "bbaabbaa" = "aabbaabbbbaabbaa"
"aa" + "a = "aaa"
"bba" + "bb" = "bbabb"
"bab" + "ab" = "babab"
"bbaa"
Also there can be 1000 such strings in total.
1) Make a graph where we have one node for each word.
2) Go through all pairs of words and check if they form palindrome if we concatenate them. If they do connect corresponding nodes in graph with edge.
3) Now use matching algorithm to find maximum number of edges you can match: http://en.wikipedia.org/wiki/Blossom_algorithm
Time complexity: O(N) for point 1, O(n*n*1000) for point 2 and O(V^4) for point 3 yielding total complexity of O(n^4).

Using dynamic programming to count the number of permutations

I have a string A of length N. I have to find number of strings (B) of length N that have M (M<=N) same characters as string A but satisfies the condition that A[i]!=B[i] for all i. Assume the characters that have to be same and the different ones are also given. What will be the recurrence relation to find number of such strings?
Example
123 is string A and M=1, and the character which is same is '1', and the new characters are '4' and '5'. The valid permutations are 451, 415, 514, 541. So it is a sort of derangement of 1 item of the given 3.
I am able to find the answer using inclusion-exclusion principle but wanted to know whether there is a recurrence relation to do the same?
Let us call g(M,N) the number of permutations satisfying your condition.
If M is 0, then the answer is N!
Otherwise, M>0 and consider placing the first character that is in string A.
There are M important positions corresponding to the places in the string where we are not allowed to place a certain character.
If we put our first character in one of these (M-1) important places (we cannot put it in position 1 due to the restriction), then we must take the place of one of the restricted characters, and so the number of restrictions reduces by 2 (1 for the character we place, and 1 for the character whose position we occupied).
If we put our first character in one of the N-M unimportant places, then we have only reduced the number of restrictions by 1.
Therefore the recurrence relation is:
g(M,N)=(M-1)g(M-2,N-1)+(N-M)g(M-1,N-1) if M>0
=N! if M=0
For your example, we wish to calculate g(1,3) (1 character matches, total of 3 characters placed)
g(1,3)=(3-1)g(0,2)
=(3-1).2!
=4

Algorithm to find

the logic behind this was (n-2)3^(n-3) has lots of repetitons like (abc)***(abc) when abc is at start and at end and the strings repated total to 3^4 . similarly as abc moves ahead and number of sets of (abc) increase
You can use dynamic programming to compute the number of forbidden strings.
The algorithms follow from the observation below:
"Legal string of size n is the legal string of size n - 1 extended with one letter, so that the last three letters of the resulting string are not all distinct."
So if we had all the legal strings of size n-1 we could try extending them to obtain the legal strings of size n.
To check whether the extended string is legal we just need to know the last two letters of the previous string (of size n-1).
In the algorithm we will compute two arrays, where
different[i] # number of legal strings of length i in which last two letters are different
same[i] # number of legal strings of length i in which last two letters are the same
It can be easily proved that:
different[i+1] = different[i] + 2*same[i]
same[i+1] = different[i] + same[i]
It is the consequence of the following facts:
Any 'same' string of size i+1 can be obtained either from 'same' string of size i (think BB -> BBB) or from 'different' string (think AB -> ABB) and these are the only options.
Any 'different' string of size i+1 can be obtained either from 'different' string of size i (think AB-> ABA ) or from the 'same' string in two ways (AA -> AAB or AA -> AAC)
Having observed all this it is easy to write an algorithm that computes the result in O(n) time.
I suggest you use recursion, and look at two numbers:
F(n), the number of legal strings of length n whose last two symbols are the same.
G(n), the number of legal strings of length n whose last two symbols are different.
Is that enough to go on?
get the ASCII values of the last three letters and add the square values of these letters. If it gives a certain result, then it is forbidden. For A, B and C, it would be fine.
To do this:
1) find out how to get characters from your string.
2) find out how to get ASCII value of a character.
3) Multiply these ASCII values with themselves.
4) Do that for the three letters each time and add their values.

Resources