Print the final string when minimum value strings get concatenated in every operation
Last Updated :
08 Mar, 2023
Given an array of strings and an array of integers where ith integer of the array corresponds to the value of the ith string present in the string array. Now pick two strings that have the smallest values in the integer array and sum up both the integers and concatenate the strings and add both the summed up integer to the integer array and the concatenated string to the string array, and keep repeating the whole process until only a single element is left in both the arrays. Also, note that once any two strings and integers are picked they get deleted from the array and new strings and integers are added back to the respective arrays.
The task is to print the final string obtained.
Examples:
Input: str = {“Geeks”, “For”, “Geeks”}, num = {2, 3, 7}
Output: GeeksForGeeks
Pick 2 and 3 add them, and form “GeeksFor” and 5
Add them back to the arrays, str = {“GeeksFor”, “Geeks”}, num = {5, 7}
Now pick 7 and 5 add them to form “GeeksForGeeks” which is the final string.
Input: str = {“abc”, “def”, “ghi”, “jkl”}, num = {1, 4, 2, 6}
Output: jklabcghidef
Approach: The idea is to use a priority queue, and make a pair of strings and their corresponding values and store them in the priority queue and keep dequeuing two pairs from the priority queue, adding the integers and concatenating the strings, and enqueuing them back into the priority queue, till the size of the queue reduces to one, and then print the only remaining string in the queue.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Priority
{
string s;
int count;
};
struct Compare
{
int operator()(Priority a, Priority b)
{
if (a.count > b.count)
return 1;
else if (a.count < b.count)
return -1;
return 0;
}
};
static void FindString(string str[], int num[],
int n)
{
priority_queue< int , vector<Priority>, Compare> p;
for ( int i = 0; i < n; i++)
{
Priority x;
x.s = str[i];
x.count = num[i];
p.push(x);
}
while (p.size() > 1)
{
Priority x = p.top();
p.pop();
Priority y = p.top();
p.pop();
int temp = x.count + y.count;
string sb = x.s + y.s;
Priority z;
z.count = temp;
z.s = sb;
p.push(z);
}
Priority z = p.top();
p.pop();
cout << z.s << endl;
}
int main()
{
string str[] = { "Geeks" , "For" , "Geeks" };
int num[] = { 2, 3, 7 };
int n = sizeof (num) / sizeof ( int );
FindString(str, num, n);
}
|
Java
import java.util.*;
import java.lang.*;
class Priority {
String s;
int count;
}
class PQ implements Comparator<Priority> {
public int compare(Priority a, Priority b)
{
if (a.count > b.count)
return 1 ;
else if (a.count < b.count)
return - 1 ;
return 0 ;
}
}
class GFG {
static void FindString(String str[], int num[], int n)
{
Comparator<Priority> comparator = new PQ();
PriorityQueue<Priority> p
= new PriorityQueue<Priority>(comparator);
for ( int i = 0 ; i < n; i++) {
Priority x = new Priority();
x.s = str[i];
x.count = num[i];
p.add(x);
}
while (p.size() > 1 ) {
Priority x = p.poll();
p.remove(x);
Priority y = p.poll();
p.remove(y);
int temp = x.count + y.count;
String sb = x.s + y.s;
Priority z = new Priority();
z.count = temp;
z.s = sb;
p.add(z);
}
Priority z = p.poll();
System.out.println(z.s);
}
public static void main(String[] args)
{
String str[] = { "Geeks" , "For" , "Geeks" };
int num[] = { 2 , 3 , 7 };
int n = num.length;
FindString(str, num, n);
}
}
|
Python3
def FindString( str , num, n):
p = []
for i in range (n):
x = [ 0 , 0 ]
x[ 0 ] = str [i]
x[ 1 ] = num[i]
p.append(x)
while ( len (p) > 1 ):
x = p[ - 1 ]
p.pop()
y = p[ - 1 ]
p.pop()
temp = x[ 1 ] + y[ 1 ]
sb = x[ 0 ] + y[ 0 ]
z = [ 0 , 0 ]
z[ 1 ] = temp
z[ 0 ] = sb
p.append(z)
z = p[ - 1 ]
p.pop()
print (z[ 0 ])
str = [ "Geeks" , "For" , "Geeks" ]
num = [ 2 , 3 , 7 ]
n = len (num)
FindString( str , num, n);
|
Javascript
<script>
function FindString(str, num, n)
{
var p = [];
for ( var i = 0; i < n; i++)
{
var x = [0,0];
x[0] = str[i];
x[1] = num[i];
p.push(x);
}
while (p.length > 1)
{
var x = p[p.length-1];
p.pop();
var y = p[p.length-1];
p.pop();
var temp = x[1] + y[1];
var sb = x[0] + y[0];
var z = [0,0];
z[1] = temp;
z[0] = sb;
p.push(z);
}
var z = p[p.length-1];
p.pop();
document.write(z[0] + "<br>" );
}
var str = [ "Geeks" , "For" , "Geeks" ];
var num = [2, 3, 7];
var n = num.length;
FindString(str, num, n);
</script>
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void FindString( string [] str, int [] num, int n)
{
var p = new List<Tuple< string , int >>();
for ( int i = 0; i < n; i++)
{
var x = new Tuple< string , int >(str[i], num[i]);
p.Add(x);
}
while (p.Count > 1)
{
var x = p[p.Count - 1];
p.RemoveAt(p.Count - 1);
var y = p[p.Count - 1];
p.RemoveAt(p.Count - 1);
var temp = x.Item2 + y.Item2;
var sb = x.Item1 + y.Item1;
var z = new Tuple< string , int >(sb, temp);
p.Add(z);
}
var z1 = p[p.Count - 1];
p.RemoveAt(p.Count - 1);
Console.WriteLine(z1.Item1);
}
static void Main( string [] args)
{
string [] str = { "Geeks" , "For" , "Geeks" };
int [] num = { 2, 3, 7 };
int n = num.Length;
FindString(str, num, n);
}
}
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...