Open In App

Smallest string containing all unique characters from given array of strings

Last Updated : 17 Feb, 2023
Comments
Improve
Suggest changes
2 Likes
Like
Report

Given an array of strings arr[], the task is to find the smallest string which contains all the characters of the given array of strings.

Examples: 

Input: arr[] = {"your", "you", "or", "yo"}
Output: ruyo
Explanation: The string "ruyo" is the smallest string which contains all the characters that are used across all the strings of the given array. 

Input: arr[] = {"abm", "bmt", "cd", "tca"}
Output: abctdm

Approach: This problem can be solved by using the Set Data Structure. Set has the capability to remove duplicates, which is needed in this problem in order to minimize the string size. Add all the characters in the set from all the strings in the array arr[] and form a string containing all the characters remaining in the set, which is the required answer. 

Below is the implementation of the above approach.

C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;

string minSubstr(vector<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.size(); i++)
    {
        str += s[i];
    }

    // Set to store the characters
    unordered_set<char> 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<string> arr = {"your", "you",
                          "or", "yo"};

    cout << (minSubstr(arr));
    return 0;
}

// This code is contributed by Potta Lokesh
Java Python3 C# JavaScript

Output
ruoy

 Time Complexity: O(N*M), where M is the average length of strings in the given array
Auxiliary Space: O(N) because extra space for string str is being used

Approach #2: This problem can also be solved by using the Map Data Structure. Map stores all the characters present in the string with their occurrence. After iterating on the map we will get the all unique characters.

C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;

string minSubstr(vector<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.size(); i++) {
        str += s[i];
    }

    // map to store the characters with frequency
    unordered_map<char, int> mp;

    // Loop to iterate over all
    // the characters in str
    for (int i = 0; i < str.length(); i++) {
        mp[str[i]]++;
    }
    string res = "";

    // Loop to iterate over the map
    for (auto it : mp) {
        res += it.first;
    }

    // Return Answer
    return res;
}

// Driver Code
int main()
{
    vector<string> arr = { "your", "you", "or", "yo" };

    cout << (minSubstr(arr));
    return 0;
}

// This code is contributed by Prasad Kandekar(prasad264)
Java Python3 C# JavaScript

Output:

ruoy

Complexity analysis:

Time Complexity: O(N*M), where M is the average length of strings in the given array
Auxiliary Space: O(N) because extra space for string str and unordered_map are being used


Next Article
Practice Tags :

Similar Reads