找到可以生成所有给定字符的最小字符串
给定一个字符串数组arr[]。任务是生成包含数组中所有字符串的所有字符且大小最小的字符串。可能有许多这样的可能字符串,任何一个都是可以接受的。
例子:
Input: arr[] = {“your”, “you”, “or”, “yo”}
Output: ruyo
Explanation: The string “ruyo” is the string which contains all the characters present in all the strings in arr[].
There can be many other strings of size 4 e.g. “oury”. Those are also acceptable.
Input: arr[] = {“abm”, “bmt”, “cd”, “tca”}
Output: abctdm
方法:这个问题可以通过使用Set Data Structure来解决。 Set 具有删除重复项的能力,这在此问题中是必需的,以最小化字符串大小。从数组arr[]中的所有字符串中添加集合中的所有字符,并形成一个包含集合中剩余的所有字符的字符串,这就是所需的答案。
下面是上述方法的实现。
C++
// C++ code for the above approach
#include
using namespace std;
string minSubstr(vector s)
{
// Stores the concatenated string
// of all the given strings
string str = "";
// Loop to iterate through all
// the given strings
for (int i = 0; i < s.size(); i++)
{
str += s[i];
}
// Set to store the characters
unordered_set set;
// Loop to iterate over all
// the characters in str
for (int i = 0; i < str.length(); i++)
{
set.insert(str[i]);
}
string res = "";
// Loop to iterate over the set
for (auto itr = set.begin(); itr != set.end(); itr++)
{
res = res + (*itr);
}
// Return Answer
return res;
}
// Driver Code
int main()
{
vector arr = {"your", "you",
"or", "yo"};
cout << (minSubstr(arr));
return 0;
}
// This code is contributed by Potta Lokesh
Java
// Java program to implement above approach
import java.util.*;
public class GfG {
public static String minSubstr(String s[])
{
// Stores the concatenated string
// of all the given strings
String str = "";
// Loop to iterate through all
// the given strings
for (int i = 0; i < s.length; i++) {
str += s[i];
}
// Set to store the characters
Set set =
new HashSet();
// Loop to iterate over all
// the characters in str
for (int i = 0; i < str.length();
i++) {
set.add(str.charAt(i));
}
// Stores the required answer
String res = "";
Iterator itr =
set.iterator();
// Loop to iterate over the set
while (itr.hasNext()) {
res += itr.next();
}
// Return Answer
return res;
}
// Driver Code
public static void main(String[] args)
{
String arr[]
= new String[] { "your", "you",
"or", "yo" };
System.out.println(minSubstr(arr));
}
}
Python3
# Python program to implement above approach
def minSubstr(s):
# Stores the concatenated string
# of all the given strings
str = "";
# Loop to iterate through all
# the given strings
for i in range(len(s)):
str += s[i];
# Set to store the characters
setv = set();
# Loop to iterate over all
# the characters in str
for i in range(len(str)):
setv.add(str[i]);
# Stores the required answer
res = "";
# Loop to iterate over the set
for itr in setv:
res += itr;
# Return Answer
return res;
# Driver Code
if __name__ == '__main__':
arr = ["your", "you", "or", "yo"];
print(minSubstr(arr));
# This code is contributed by 29AjayKumar
C#
// C# program to implement above approach
using System;
using System.Collections.Generic;
public class GfG {
public static String minSubstr(String []s)
{
// Stores the concatenated string
// of all the given strings
String str = "";
// Loop to iterate through all
// the given strings
for (int i = 0; i < s.Length; i++) {
str += s[i];
}
// Set to store the characters
HashSet set =
new HashSet();
// Loop to iterate over all
// the characters in str
for (int i = 0; i < str.Length;
i++) {
set.Add(str[i]);
}
// Stores the required answer
String res = "";
// Loop to iterate over the set
foreach (char itr in set) {
res += itr;
}
// Return Answer
return res;
}
// Driver Code
public static void Main(String[] args)
{
String []arr
= new String[] { "your", "you",
"or", "yo" };
Console.WriteLine(minSubstr(arr));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
ruyo
时间复杂度: O(N*M),其中 M 是给定数组中字符串的平均长度
辅助空间: O(1)