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.
Examples:
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.
Below is the implementation of above approach.
C++
// C++ implementation to find the minimum length of // string having all permutation of the given string #include <bits/stdc++.h> using namespace std; // function to find minimum length of required string void minLength(string s) { // Precomputed answers for all String. vector<string> minlen = { "0", "1", "3", "9", "33", "153", "872", "5912", "46232", "409112", "4037912", "43954712", "522956312", "6749977112", "93928268312", "1401602636312", "22324392524312", "378011820620312", "6780385526348312", "128425485935180312", "2561327494111820312", "53652269665821260312", "1177652997443428940312", "27029669736328405580312", "647478071469567844940312", "16158688114800553828940312", "419450149241406189412940312" }; cout << minlen[s.size()]; } // Driver program int main() { string s = "abc"; // function call to print minimum length of string minLength(s); return 0; } // This code is written by // Sanjit_Prasad |
Java
// Java implementation to find // the minimum length of string // having all permutation of // the given string class GFG { // function to find minimum // length of required string static void minLength(String s) { // Precomputed answers for all String. String minlen[] = { "0", "1", "3", "9", "33", "153", "872", "5912", "46232", "409112", "4037912", "43954712", "522956312", "6749977112", "93928268312", "1401602636312", "22324392524312", "378011820620312", "6780385526348312", "128425485935180312", "2561327494111820312", "53652269665821260312", "1177652997443428940312", "27029669736328405580312", "647478071469567844940312", "16158688114800553828940312", "419450149241406189412940312"}; System.out.println(minlen[s.length()]); } // Driver code public static void main (String args[]) { String s = "abc"; // function call to print // minimum length of string minLength(s); } } // This code is contributed by ANKITRAI1 |
Python
# Python implementation to find the minimum length of # string having all permutation of the given string. # function to find minimum length of required string. def minLength(s): # Precomputed answers for all String. minlen = ["0", "1", "3", "9", "33", "153", "872", "5912", "46232", "409112", "4037912", "43954712", "522956312", "6749977112", "93928268312", "1401602636312", "22324392524312", "378011820620312", "6780385526348312", "128425485935180312", "2561327494111820312", "53652269665821260312", "1177652997443428940312", "27029669736328405580312", "647478071469567844940312", "16158688114800553828940312", "419450149241406189412940312"] print(minlen[len(s)]) # Driver program s = "abc" # function call to print minimum length of string minLength(s) # This code is written by # Sanjit_Prasad |
C#
// C# implementation to find // the minimum length of string // having all permutation of // the given string using System; class GFG { // function to find minimum // length of required string static void minLength(String s) { // Precomputed answers for all String. String[] minlen = { "0", "1", "3", "9", "33", "153", "872", "5912", "46232", "409112", "4037912", "43954712", "522956312", "6749977112", "93928268312", "1401602636312", "22324392524312", "378011820620312", "6780385526348312", "128425485935180312", "2561327494111820312", "53652269665821260312", "1177652997443428940312", "27029669736328405580312", "647478071469567844940312", "16158688114800553828940312", "419450149241406189412940312"}; Console.WriteLine(minlen[s.Length]); } // Driver code public static void Main () { String s = "abc"; // function call to print // minimum length of string minLength(s); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
PHP
<?php // PHP implementation to find the // minimum length of string having // all permutation of the given string // function to find minimum length // of required string function minLength($s) { // Precomputed answers for all String. $minlen = array("0", "1", "3", "9", "33", "153", "872", "5912", "46232", "409112", "4037912", "43954712", "522956312", "6749977112", "93928268312", "1401602636312", "22324392524312", "378011820620312", "6780385526348312", "128425485935180312", "2561327494111820312", "53652269665821260312", "1177652997443428940312", "27029669736328405580312", "647478071469567844940312", "16158688114800553828940312", "419450149241406189412940312"); echo $minlen[strlen($s)]; } // Driver Code $s = "abc"; // function call to print // minimum length of string minLength($s); // This code is written by // ash264 ?> |
Output:
9
Time Complexity: ![]()
Recommended Posts:
- Maximum even length sub-string that is permutation of a palindrome
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Reduce the string to minimum length with the given operation
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Find length of longest subsequence of one string which is substring of another string
- Check if a string contains a palindromic sub-string of even length
- Find the character in first string that is present at minimum index in second string
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum number of given operations required to convert a string to another string
- Convert string X to an anagram of string Y with minimum replacements
- Minimum changes required to make first string substring of second string
- Lexicographically n-th permutation of a string
- Number of distinct permutation a String can have
- Find n-th lexicographically permutation of a string | Set 2
- String which when repeated exactly K times gives a permutation of S
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



