# Print the final string when minimum value strings get concatenated in every operation

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 which 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 dequeueing 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:

 `// Java implementation of the approach ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `// Class that represents a pair ` `class` `Priority { ` `    ``String s; ` `    ``int` `count; ` `} ` ` `  `class` `PQ ``implements` `Comparator { ` `    ``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 { ` ` `  `    ``// Function that prints the final string ` `    ``static` `void` `FindString(String str[], ``int` `num[], ``int` `n) ` `    ``{ ` `        ``Comparator comparator = ``new` `PQ(); ` `        ``PriorityQueue p ` `            ``= ``new` `PriorityQueue(comparator); ` ` `  `        ``// Add all the strings and their corresponding ` `        ``// values to the priority queue ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` `            ``Priority x = ``new` `Priority(); ` `            ``x.s = str[i]; ` `            ``x.count = num[i]; ` `            ``p.add(x); ` `        ``} ` ` `  `        ``// Take those two strings from the priority ` `        ``// queue whose corresponding integer values are smaller ` `        ``// and add them up as well as their values and ` `        ``// add them back to the priority queue ` `        ``// while there are more than a single element in the queue ` `        ``while` `(p.size() > ``1``) { ` ` `  `            ``// Get the minium valued string ` `            ``Priority x = p.poll(); ` `            ``p.remove(x); ` ` `  `            ``// Get the second minium valued string ` `            ``Priority y = p.poll(); ` `            ``p.remove(y); ` ` `  `            ``// Updated integer value ` `            ``int` `temp = x.count + y.count; ` `            ``String sb = x.s + y.s; ` ` `  `            ``// Create new entry for the queue ` `            ``Priority z = ``new` `Priority(); ` `            ``z.count = temp; ` `            ``z.s = sb; ` ` `  `            ``// Add to the queue ` `            ``p.add(z); ` `        ``} ` ` `  `        ``// Print the only remaining string ` `        ``Priority z = p.poll(); ` `        ``System.out.println(z.s); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String str[] = { ``"Geeks"``, ``"For"``, ``"Geeks"` `}; ` `        ``int` `num[] = { ``2``, ``3``, ``7` `}; ` `        ``int` `n = num.length; ` ` `  `        ``FindString(str, num, n); ` `    ``} ` `} `

Output:

```GeeksForGeeks
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.