给定一个字符串在哪里, 。假设其中的所有字符是独一无二的任务是计算一个字符串的最小长度,该长度由给定字符串的所有排列以任意顺序组成。
注意:所有排列都必须在结果字符串作为子字符串出现。
例子:
Input : ab
Output : 3
The resulting string is aba.
Input : abc
Output : 9
The resulting string is abcabacba.
方法:以上问题的答案很简单。
- 如果字符串的长度为1 ,则答案为1 。
- 如果字符串的长度为2 ,则答案为3 。
- 如果字符串的长度为3 ,则答案为9 。
因此,观察输出后,我们可以看到,如果字符串的长度为n ,则答案将为1! + 2! +…+ n! 。因此,我们可以在字符串向量中预先计算结果至n = 26 。
下面是上述方法的实现。
C++
// C++ implementation to find the minimum length of
// string having all permutation of the given string
#include
using namespace std;
// function to find minimum length of required string
void minLength(string s)
{
// Precomputed answers for all String.
vector 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
Javascript
输出:
9
时间复杂度: