Find smallest string with whose characters all given Strings can be generated
Last Updated :
30 Sep, 2022
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: 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
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++
#include <bits/stdc++.h>
using namespace std;
string minSubstr(vector<string> s)
{
string str = "" ;
for ( int i = 0; i < s.size(); i++)
{
str += s[i];
}
unordered_set< char > set;
for ( int i = 0; i < str.length(); i++)
{
set.insert(str[i]);
}
string res = "" ;
for ( auto itr = set.begin(); itr != set.end(); itr++)
{
res = res + (*itr);
}
return res;
}
int main()
{
vector<string> arr = { "your" , "you" ,
"or" , "yo" };
cout << (minSubstr(arr));
return 0;
}
|
Java
import java.util.*;
public class GfG {
public static String minSubstr(String s[])
{
String str = "" ;
for ( int i = 0 ; i < s.length; i++) {
str += s[i];
}
Set<Character> set =
new HashSet<Character>();
for ( int i = 0 ; i < str.length();
i++) {
set.add(str.charAt(i));
}
String res = "" ;
Iterator<Character> itr =
set.iterator();
while (itr.hasNext()) {
res += itr.next();
}
return res;
}
public static void main(String[] args)
{
String arr[]
= new String[] { "your" , "you" ,
"or" , "yo" };
System.out.println(minSubstr(arr));
}
}
|
Python3
def minSubstr(s):
str = "";
for i in range ( len (s)):
str + = s[i];
setv = set ();
for i in range ( len ( str )):
setv.add( str [i]);
res = "";
for itr in setv:
res + = itr;
return res;
if __name__ = = '__main__' :
arr = [ "your" , "you" , "or" , "yo" ];
print (minSubstr(arr));
|
C#
using System;
using System.Collections.Generic;
public class GfG {
public static String minSubstr(String []s)
{
String str = "" ;
for ( int i = 0; i < s.Length; i++) {
str += s[i];
}
HashSet< char > set =
new HashSet< char >();
for ( int i = 0; i < str.Length;
i++) {
set .Add(str[i]);
}
String res = "" ;
foreach ( char itr in set ) {
res += itr;
}
return res;
}
public static void Main(String[] args)
{
String []arr
= new String[] { "your" , "you" ,
"or" , "yo" };
Console.WriteLine(minSubstr(arr));
}
}
|
Javascript
<script>
public class GfG
{
function minSubstr(s)
{
var str = "" ;
for ( var i = 0; i < s.length; i++) {
str += s[i];
}
var set = new Set();
for ( var i = 0; i < str.length;
i++) {
set.add(str.charAt(i));
}
var res = "" ;
for (let itr of set){
res += itr;
}
return res;
}
var arr = [ "your" , "you" ,
"or" , "yo" ];
document.write(minSubstr(arr));
</script>
|
Time Complexity: O(N*M), where M is the average length of strings in the given array
Auxiliary Space: O(N*M)
Share your thoughts in the comments
Please Login to comment...