I came up with a solution for this in 84+(CE) Basic. Note that if you have multiple words they must be entered without spaces.
Code: Input "WORD=?",Str1
Prompt N
Delvar L₃length(Str1→dim(L₃
For(I,1,Ans
inString(Str1,sub(Str1,I,1
1+L₃(Ans→L₃(Ans
End
{N!→L₁
For(I,1,dim(L₃
Ans→L₂
For(J,1,L₃(I
augment({0},L₂/J→L₂
Ans+augment(L₁,{0→L₁
End
End
Ans(N+1
Perhaps out of habit I wrote this in an almost obfuscatedly optimized way, but let's break down what's going on here.
First, I delete L₃ and set its size to the length of the word (this ensures it is initialized to all zeros). This list will be used to accumulate the frequency of each letter.
Code: Delvar L₃length(Str1→dim(L₃
Next, loop I from 1 to the length of the string (which is in Ans from the previous line).
Code: For(I,1,Ans
Now, find the index of the first occurence of the I-th letter in the word. This effectively maps each distinct letter to a unique integer.
Code: inString(Str1,sub(Str1,I,1
Now that we have the mapped index, increment the corresponding element of L₃, and loop to the next letter.
Code: 1+L₃(Ans→L₃(Ans
End
At this point, each non-zero element of L₃ holds a letter frequency. For example, in the case of STATEMEET, it would be {1,3,1,0,3,1,0,0,0}.
Now, we'll use lists to represent polynomials for the generating function approach. Index 1 of a list will be the constant term, index 2 will be the coefficient of x, index 3 will be the coefficient of x², and so on.
We start with the polynomial (which we'll call P) with N! as a constant term in L₁.
Code: {N!→L₁
Now, we loop over each letter frequency to multiply its corresponding generating polynomial.
Code: For(I,1,dim(L₃
Here, we initialize L₂ to P (which is in Ans) times x^0 / 0! (that is, the constant term of 1 present in each generating polynomial).
Code: Ans→L₂
Now, we loop J from 1 to the current letter frequency to add in P times x^J / J!. (This loop is implicitly skipped for any 0-frequency element of L₃.)
Code: For(J,1,L₃(I
At the moment, L₂ is P times x^(J-1) / (J-1)!. To transform this into P times x^J / J!, we prepend a 0 (effectively multiplying by x) and divide by J.
Code: augment({0},L₂/J→L₂
Here, we add the resulting polynomial to L₁ (to which we append a 0 so the number of list elements match), and loop to the next exponent.
Code: Ans+augment(L₁,{0→L₁
End
Now L₁ (and Ans) is the new value of P, so loop to the next letter frequency.
Code: End
Finally, we extract the coefficient of x^N from the resulting polynomial.
Code: Ans(N+1
Let me know if you have any questions!