Find smallest string with whose characters all given Strings can be generated Last Updated : 30 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given an array of strings arr[]. The task is to generate the string which contains all the characters of all the strings present in array and smallest in size. There can be many such possible strings and any one is acceptable. Examples: Input: arr[] = {"your", "you", "or", "yo"}Output: ruyoExplanation: 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 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 // 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<Character> set = new HashSet<Character>(); // 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<Character> 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<char> set = new HashSet<char>(); // 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 <script> // javascript program to implement above approach public class GfG { function minSubstr(s) { // Stores the concatenated string // of all the given strings var str = ""; // Loop to iterate through all // the given strings for (var i = 0; i < s.length; i++) { str += s[i]; } // Set to store the characters var set = new Set(); // Loop to iterate over all // the characters in str for (var i = 0; i < str.length; i++) { set.add(str.charAt(i)); } // Stores the required answer var res = ""; // Loop to iterate over the set for(let itr of set){ res += itr; } // Return Answer return res; } // Driver Code var arr = [ "your", "you", "or", "yo" ]; document.write(minSubstr(arr)); // This code is contributed by 29AjayKumar </script> Outputruyo Time Complexity: O(N*M), where M is the average length of strings in the given arrayAuxiliary Space: O(N*M) Comment More infoAdvertise with us Next Article Find smallest string with whose characters all given Strings can be generated A akshitsaxenaa09 Follow Improve Article Tags : Strings Searching Mathematical Hash Algo Geek DSA Algo-Geek 2021 cpp-set frequency-counting +5 More Practice Tags : HashMathematicalSearchingStrings Similar Reads Generate a string whose all K-size substrings can be concatenated to form the given string Given a string str of size N and an integer K, the task is to generate a string whose substrings of size K can be concatenated to form the given string. Examples: Input: str = "abbaaa" K = 2 Output: abaa Explanation: All substring of size 2 of parent string "abaa" are "ab", "ba" and "aa". After conc 5 min read Check if a string can be split into two strings with same number of K-frequent characters Given a string S and an integer K, the task is to check if it is possible to distribute these characters into two strings such that the count of characters having a frequency K in both strings is equal. If it is possible, then print a sequence consisting of 1 and 2, which denotes which character sho 11 min read Check if a two character string can be made using given words Given a string of two characters and n distinct words of two characters. The task is to find if it is possible to arrange given words in such a way that the concatenated string has the given two character string as a substring. We can append a word multiple times. Examples: Input : str = "ya" words[ 6 min read Find the character made by adding all the characters of the given string Given a string str consisting of lowercase English alphabets. The task is to add all the character values i.e. 'a' = 1, 'b' = 2, 'c' = 3, ..., 'z' = 26 and output the character corresponding to the sum value. If it exceeds 26 then take sum % 26. Examples: Input: str = "gfg" Output: t (g + f + g) = 7 7 min read Smallest window in a String containing all characters of other String Given two strings s (length m) and p (length n), the task is to find the smallest substring in s that contains all characters of p, including duplicates. If no such substring exists, return "-1". If multiple substrings of the same length are found, return the one with the smallest starting index.Exa 15+ min read Create a string with unique characters from the given N substrings Given an array arr[] containing N substrings consisting of lowercase English letters, the task is to return the minimum length string that contains all given parts as a substring. All characters in this new answer string should be distinct. If there are multiple strings with the following property p 11 min read Generate all passwords from given character set Given a set of characters generate all possible passwords from them. This means we should generate all possible permutations of words using the given characters, with repetitions and also upto a given length. Examples: Input : arr[] = {a, b}, len = 2. Output : a b aa ab ba bb The solution is to use 8 min read Find two unique Palindrome Strings using given String characters You are given a string S of two distinct characters as input, the task is to find two strings using characters of the given string, such that both strings are different from S and both should be a palindrome. Examples: Input: S = "xyyxxxx"Output: yxxxxxy, xxyxyxxExplanation: It can be verified that 15+ min read Check if characters of a given string can be used to form any N equal strings Given a string S and an integer N, the task is to check if it is possible to generate any string N times from the characters of the given string or not. If it is possible, print Yes. Otherwise, print No. Examples: Input: S = "caacbb", N = 2Output: YesExplanation: All possible strings that can be gen 5 min read Find characters which when increased by K are present in String Given a string s of lowercase English alphabets and integer K. the task is to check if each character after increasing their ASCII by K is present in the string or not. Return only unique characters and the first index where they are present. Examples: Input: s = "dszepvaxuo", k = 3Output: {{1, 's'} 8 min read Like