Reputation: 43
I have four digits, "1", "2", "3", "4".
The input of the program is an integer which can only comprise of the above 4 digits. There is going to be a lot of inputs.
Example of inputs: 1123, 4123, 4444
I need to calculate the number of permutations of a given input that adheres to the following rules:
Could I use any type of memoization to solve this problem? How do i store it in a 2d array? Do give tips thanks!
Upvotes: 2
Views: 2688
Reputation: 43738
Since you are only interested in the number of allowed arrangements, most of the inputs lead to identical results.
Classifying the inputs according to digit frequencies leads to just 5 different cases for four digit inputs:
class examples
4 4444, 2222, ...
3 1 1211, 2232, ...
2 2 1331, 4422, ...
2 1 1 3413, 1123, ...
1 1 1 1 1234, 4231, ...
Once you have figured out the answer for each case, any new input can be handled very fast.
Upvotes: 2