Minimum length of string having all permutation of given string.
Last Updated :
30 Nov, 2022
Given a string S where 1\leq length\; of\; S\leq 26 . Assume that all the characters in S 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.
Implementation:
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
?>
JavaScript
<script>
// javascript 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 = [ "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"];
document.write(minlen[s.length]);
}
// Driver code
s = "abc";
// function call to print
// minimum length of string
minLength(s);
// This code is contributed by 29AjayKumar
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
All permutations of a string using iteration A permutation, also called an âarrangement numberâ or âorderâ, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation ( Source: Mathword ) Below are the permutations of string ABC. ABC ACB BAC BCA CBA CAB We hav
4 min read
Print all permutations of a string in Java Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. For instance, the words âbatâ and âtabâ represents two distinct permutation (or arrangements) of a similar three lett
3 min read
Minimum move to end operations to make all strings equal Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end. Examples: Input: n = 2, arr[] = {"molzv", "lzvmo"}Output: 2Explanation: In first string, we remove first element("m") from first s
13 min read
Permutations of a given string using STL Given a string s, the task is to return all unique permutations of a given string in lexicographically sorted order.Note: A permutation is the rearrangement of all the elements of a string.Examples:Input: s = "ABC"Output: "ABC", "ACB", "BAC", "BCA", "CBA", "CAB"Input: s = "XY"Output: "XY", "YX"Input
4 min read
Print all Unique permutations of a given string. Given a string that may contain duplicates, the task is find all unique permutations of given string in any order.Examples: Input: "ABC"Output: ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]Explanation: Given string ABC has 6 unique permutations as "ABC", "ACB", "BAC", "BCA", "CAB" and "CBA".Input: "AAA
12 min read
Time complexity of all permutations of a string Time complexity of printing all permutations of a string. void perm(String str){ perm(str, ""); } void perm(String str, String prefix){ if(str.length() == 0){ System.out.println(prefix); } else{ for(int i = 0; i < str.length(); i++){ String rem = str.substring(0, i) + str.substring(i + 1); perm(r
3 min read