📌  相关文章
📜  包含给定字符串数组中所有唯一字符的最小字符串

📅  最后修改于: 2022-05-13 01:56:05.230000             🧑  作者: Mango

包含给定字符串数组中所有唯一字符的最小字符串

给定一个字符串数组arr[] ,任务是找到包含给定字符串数组所有字符的最小字符串。

例子:

方法:这个问题可以通过使用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
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 code for the 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
    _set = set()
 
    # Loop to iterate over all
    # the characters in str
    for i in range(len(str)):
        _set.add(str[i])
 
    # Stores the required answer
    res = ""
 
    # Loop to iterate over the set
    for itr in _set:
        res += itr
 
    # Return Answer
    return res
 
# Driver Code
arr = ["your", "you", "or", "yo"]
print(minSubstr(arr))
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
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 i in set) {
            res += i;
        }
 
        // Return Answer
        return res;
    }
 
    // Driver Code
    public static void Main()
    {
        string []arr
            = { "your", "you", "or", "yo" };
 
        Console.WriteLine(minSubstr(arr));
    }
}
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
ruyo

时间复杂度: O(N*M),其中 M 是给定数组中字符串的平均长度
辅助空间: O(1)