Create a string with unique characters from the given N substrings
Last Updated :
24 Feb, 2023
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 present, you have to print the string with minimum length.
Examples:
Input: N = 3, arr[] = {“bcd”, “ab”, “cdef”}
Output: “abcdef”
Explanation:
- abcdef contains “bcd” from [1-3] index (0-based indexing)
- abcdef contains “ab” from [0-2]
- abcdef contains “cdef” from [2-5]
It can be proven that “abcdef” is the smallest string consisting of the given 3 substring fragments from input.
Input: N = 3, arr[]= {“dfghj”, “ghjkl”, “asdfg”];
Output: “asdfghjkl”
Explanation:
- asdfghjkl contains “dfghj” from [2-6] index (0-based indexing)
- asdfghjkl contains “ghjkl” from [4-8]
- asdfghjkl contains “asdfg” from [0-4]
It can be proven that “asdfghjkl ” is the smallest string consisting of the given 3 substring fragments from input.
Approach: Implement the below idea to solve the problem:
- While traversing a string, if a character in left and right exists we will be storing this, information in the left[] and right[] array. If an element of a substring does not have a left element,
this means that it’s the first element. We will be marking these elements as -1(indicating the start of a substring).
- Finally, print the final answer. We will be starting with the character having a – 1 value in the left[] array because it’s the start of the string and will gonna print its right element from the right[] array, now this right element is the new element and now we will be finding it’s the right element from right[] array the same way until we reach -2 (as right) for an element, which indicates the end of the string.
Follow the steps to solve the above idea:
- Create two arrays left[] and right[] having size = 26 (a to z)
- Initialize each value for left[] and right[] = -2 (indicating the following characters has no left and right elements yet)
- Iterate over each substring given in the input array
- if it’s the first character mark it’s left = -1 (indicating the start of a substring)
- else if, it’s left == -2 (does not exist yet) update it with the left character if it exists in the current substring
- update the right[] value by accessing s[i+1]th character of the current substring if it exists.
- To print the final result find out the index containing -2 in the left[] array
- print this element and update current = right[current]
- repeat until right != -2
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void formedString( int n, vector<string>& arr)
{
vector< int > left(26), right(26);
for ( int i = 0; i < 26; i++)
left[i] = right[i] = -2;
for (string s : arr) {
for ( int j = 0; j < s.size(); j++) {
int t = s[j] - 'a' ;
if (j > 0) {
int t1 = s[j - 1] - 'a' ;
left[t] = t1;
}
else if (left[t] == -2)
left[t] = -1;
if (j < s.size() - 1) {
int t1 = s[j + 1] - 'a' ;
right[t] = t1;
}
}
}
int j;
for ( int i = 0; i < 26; i++)
if (left[i] == -1) {
int j = i;
while (j != -2) {
cout << ( char )(j + 97);
j = right[j];
}
}
}
int main()
{
int n = 3;
vector<string> arr = { "dfghj" , "ghjkl" , "asdfg" };
formedString(n, arr);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void formedString( int n, String arr[])
{
int [] left = new int [ 26 ];
int [] right = new int [ 26 ];
for ( int i = 0 ; i < 26 ; i++)
left[i] = right[i] = - 2 ;
for (String s : arr) {
for ( int j = 0 ; j < s.length(); j++) {
int t = s.charAt(j) - 'a' ;
if (j > 0 ) {
int t1 = s.charAt(j - 1 ) - 'a' ;
left[t] = t1;
}
else if (left[t] == - 2 )
left[t] = - 1 ;
if (j < s.length() - 1 ) {
int t1 = s.charAt(j + 1 ) - 'a' ;
right[t] = t1;
}
}
}
int j;
for ( int i = 0 ; i < 26 ; i++)
if (left[i] == - 1 ) {
j = i;
while (j != - 2 ) {
System.out.print(( char )(j + 97 ));
j = right[j];
}
}
}
public static void main(String[] args)
{
int n = 3 ;
String[] arr = { "dfghj" , "ghjkl" , "asdfg" };
formedString(n, arr);
}
}
|
Python3
def formedstring(n, arr):
left = [ - 2 for i in range ( 26 )]
right = [ - 2 for i in range ( 26 )]
for s in arr:
for j in range ( len (s)):
t = ord (s[j]) - ord ( 'a' )
if j > 0 :
t1 = ord (s[j - 1 ]) - ord ( 'a' )
left[t] = t1
elif left[t] = = - 2 :
left[t] = - 1
if j < len (s) - 1 :
t1 = ord (s[j + 1 ]) - ord ( 'a' )
right[t] = t1
j = None
for i in range ( 26 ):
if left[i] = = - 1 :
j = i
while j ! = - 2 :
print ( chr (j + 97 ), end = "")
j = right[j]
if __name__ = = "__main__" :
n = 3
arr = [ "dfghj" , "ghjkl" , "asdfg" ]
formedstring(n, arr)
|
C#
using System;
public class GFG{
static void formedString( int n, string [] arr)
{
int [] left = new int [26];
int [] right = new int [26];
for ( int i = 0; i < 26; i++)
left[i] = right[i] = -2;
foreach ( string s in arr) {
for ( int k = 0; k < s.Length;k++) {
int t = s[k] - 'a' ;
if (k > 0) {
int t1 = s[k - 1] - 'a' ;
left[t] = t1;
}
else if (left[t] == -2)
left[t] = -1;
if (k < s.Length - 1) {
int t1 = s[k + 1] - 'a' ;
right[t] = t1;
}
}
}
int j;
for ( int i = 0; i < 26; i++)
if (left[i] == -1) {
j = i;
while (j != -2) {
Console.Write(( char )(j + 97));
j = right[j];
}
}
}
static public void Main (){
int n = 3;
String[] arr = { "dfghj" , "ghjkl" , "asdfg" };
formedString(n, arr);
}
}
|
Javascript
function formedString(n, arr) {
let left = Array(26).fill(-2);
let right = Array(26).fill(-2);
arr.forEach((s) => {
for (let j = 0; j < s.length; j++) {
let t = s.charCodeAt(j) - 'a' .charCodeAt(0);
if (j > 0) {
let t1 = s.charCodeAt(j - 1) - 'a' .charCodeAt(0);
left[t] = t1;
} else if (left[t] == -2) {
left[t] = -1;
}
if (j < s.length - 1) {
let t1 = s.charCodeAt(j + 1) - 'a' .charCodeAt(0);
right[t] = t1;
}
}
});
let j = null ;
for (let i = 0; i < 26; i++) {
if (left[i] == -1) {
j = i;
while (j != -2) {
process.stdout.write(String.fromCharCode(j + 97));
j = right[j];
}
}
}
}
if (require.main === module) {
let n = 3;
let arr = [ "dfghj" , "ghjkl" , "asdfg" ];
formedString(n, arr);
}
|
Time Complexity: O(N*C), Where C is equal to the maximum size of a substring.
Auxiliary Space: O(1) (two extra arrays left[] and right[] of size 26 are used which is constant for all input sizes).
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...