给定n个字符串,以产生字典上最小的字符串的顺序将它们连接起来。
例子:
Input : a[] = ["c", "cb", "cba"]
Output : cbacbc
Possible strings are ccbcba, ccbacb,
cbccba, cbcbac, cbacbc and cbaccb.
Among all these strings, cbacbc is
the lexicographically smallest.
Input : a[] = ["aa", "ab", "aaa"]
Output : aaaaaab
有人可能会认为,按照字典顺序对给定的字符串进行排序,然后将它们串联起来,可以得出正确的输出。这种方法会为[[a],“ ab”,“ abc”]之类的输入产生正确的输出。但是,将此方法应用于[“ c”,“ cb”,“ cba”]会产生错误的输入,因此该方法是不正确的。
正确的方法是使用常规排序算法。当比较两个字符串a和b以确定是否必须交换它们时,请勿检查a在字典上是否小于b。而是检查是否在a的末尾附加b产生了字典上较小的字符串,或者在b的末尾附加a了。这种方法之所以有效,是因为我们希望连接的字符串在字典上较小,而不是各个字符串按字典顺序排列。
C++
// CPP code to find the lexicographically
// smallest string
#include
using namespace std;
// Compares two strings by checking if
// which of the two concatenations causes
// lexicographically smaller string.
bool compare(string a, string b)
{
return (a+b < b+a);
}
string lexSmallest(string a[], int n)
{
// Sort strings using above compare()
sort(a, a+n, compare);
// Concatenating sorted strings
string answer = "";
for (int i = 0; i < n; i++)
answer += a[i];
return answer;
}
// Driver code
int main()
{
string a[] = { "c", "cb", "cba" };
int n = sizeof(a)/sizeof(a[0]);
cout << lexSmallest(a, n);
return 0;
}
Java
// Java code to find the lexicographically
// smallest string
class GFG {
// function to sort the
// array of string
static void sort(String a[], int n)
{
//sort the array
for(int i = 0;i < n;i++)
{
for(int j = i + 1;j < n;j++)
{
// comparing which of the
// two concatenation causes
// lexiographically smaller
// string
if((a[i] + a[j]).compareTo(a[j] + a[i]) > 0)
{
String s = a[i];
a[i] = a[j];
a[j] = s;
}
}
}
}
static String lexsmallest(String a[], int n)
{
// Sort strings
sort(a,n);
// Concatenating sorted strings
String answer = "";
for (int i = 0; i < n; i++)
answer += a[i];
return answer;
}
// Driver code
public static void main(String args[])
{
String a[] = {"c", "cb", "cba"};
int n = 3;
System.out.println("lexiographically smallest string = "
+ lexsmallest(a, n));
}
}
// This code is contributed by Arnab Kundu
Python 3
# Python 3 code to find the lexicographically
# smallest string
def lexSmallest(a, n):
# Sort strings using above compare()
a.sort(reverse = True)
# Concatenating sorted strings
answer = ""
for i in range( n):
answer += a[i]
return answer
# Driver code
if __name__ == "__main__":
a = [ "c", "cb", "cba" ]
n = len(a)
print(lexSmallest(a, n))
# This code is contributed by ita_c
C#
// C# code to find
// the lexicographically
// smallest string
using System;
class GFG {
// function to sort the
// array of string
static void sort(String []a, int n)
{
//sort the array
for(int i = 0;i < n;i++)
{
for(int j = i + 1;j < n;j++)
{
// comparing which of the
// two concatenation causes
// lexiographically smaller
// string
if((a[i] + a[j]).CompareTo(a[j] +
a[i]) > 0)
{
String s = a[i];
a[i] = a[j];
a[j] = s;
}
}
}
}
static String lexsmallest(String []a, int n)
{
// Sort strings
sort(a,n);
// Concatenating sorted
// strings
String answer = "";
for (int i = 0; i < n; i++)
answer += a[i];
return answer;
}
// Driver code
public static void Main()
{
String []a = {"c", "cb", "cba"};
int n = 3;
Console.Write("lexiographically smallest string = "
+ lexsmallest(a, n));
}
}
// This code is contributed by nitin mittal
输出:
cbacbc
时间复杂性:上述代码运行INO(M * N * logN)的,其中N是一个字符串数量,M是一个字符串的最大长度。