Minimum length of string having all permutation of given string.
Given a string where . Assume that all the characters in are unique. The task is to compute the minimum length of a string which consists of all the permutations of the given string in any order.
Note: All permutations must be present as a substring in the resulting string.
Input : ab Output : 3 The resulting string is aba. Input : abc Output : 9 The resulting string is abcabacba.
Approach: The answer to the above problem is simple.
- If the length of string is 1, then answer is 1.
- If the length of string is 2, then answer is 3.
- If the length of string is 3, then answer is 9.
So, after observing the output we can see that if the length of the string is n, then answer will be 1! + 2! + … + n!. Hence we can precompute the result upto n = 26 in vector of strings.