Smallest string containing all unique characters from given array of strings
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++
#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]
_set = set ()
for i in range ( len ( str )):
_set.add( str [i])
res = ""
for itr in _set:
res + = itr
return res
arr = [ "your" , "you" , "or" , "yo" ]
print (minSubstr(arr))
|
C#
using System;
using System.Collections.Generic;
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 i in set ) {
res += i;
}
return res;
}
public static void Main()
{
string []arr
= { "your" , "you" , "or" , "yo" };
Console.WriteLine(minSubstr(arr));
}
}
|
Javascript
<script>
function minSubstr(s)
{
let str = "" ;
for (let i = 0; i < s.length; i++) {
str += s[i];
}
let set = new Set();
for (let i = 0; i < str.length; i++) {
set.add(str[i]);
}
let res = "" ;
for (let itr of set) {
res += itr;
}
return res;
}
let 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) 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++
#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_map< char , int > mp;
for ( int i = 0; i < str.length(); i++) {
mp[str[i]]++;
}
string res = "" ;
for ( auto it : mp) {
res += it.first;
}
return res;
}
int main()
{
vector<string> arr = { "your" , "you" , "or" , "yo" };
cout << (minSubstr(arr));
return 0;
}
|
Java
import java.util.*;
class GFG {
public static String minSubstr(List<String> s)
{
String str = "" ;
for (String x : s) {
str += x;
}
Map<Character, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < str.length(); i++) {
char c = str.charAt(i);
if (mp.containsKey(c)) {
mp.put(c, mp.get(c) + 1 );
}
else {
mp.put(c, 1 );
}
}
StringBuilder res = new StringBuilder();
for (Map.Entry<Character, Integer> entry :
mp.entrySet()) {
res.append(entry.getKey());
}
return res.toString();
}
public static void main(String[] args)
{
List<String> arr
= Arrays.asList( "your" , "you" , "or" , "yo" );
System.out.println(minSubstr(arr));
}
}
|
Python3
def min_substr(s):
str = ""
for i in range ( len (s)):
str + = s[i]
mp = {}
for i in range ( len ( str )):
if str [i] in mp:
mp[ str [i]] + = 1
else :
mp[ str [i]] = 1
res = ""
for key in mp:
res + = key
return res
arr = [ "your" , "you" , "or" , "yo" ]
print (min_substr(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static string MinSubstr(List< string > s)
{
string str = "" ;
for ( int i = 0; i < s.Count; i++) {
str += s[i];
}
Dictionary< char , int > mp
= new Dictionary< char , int >();
for ( int i = 0; i < str.Length; i++) {
if (mp.ContainsKey(str[i])) {
mp[str[i]]++;
}
else {
mp[str[i]] = 1;
}
}
string res = "" ;
foreach ( var item in mp) { res += item.Key; }
return res;
}
static public void Main( string [] args)
{
List< string > arr = new List< string >() {
"your" , "you" , "or" , "yo"
};
Console.WriteLine(MinSubstr(arr));
}
}
|
Javascript
function minSubstr(s) {
var str = "" ;
for ( var i = 0; i < s.length; i++) {
str += s[i];
}
var mp = new Map();
for ( var i = 0; i < str.length; i++) {
if (mp.has(str[i])) {
mp.set(str[i], mp.get(str[i]) + 1);
}
else {
mp.set(str[i], 1);
}
}
var res = "" ;
for ( var [key, value] of mp) {
res += key;
}
return res;
}
var arr = [ "your" , "you" , "or" , "yo" ];
console.log(minSubstr(arr));
|
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
Last Updated :
17 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...