Given a string **str** consisting of lowercase alphabets and an integer **K**, you can perform the following operations on **str**

- Initialize an empty string
**X = “”**. - Take any character from the first
**K**characters of**str**and append it to**X**. - Remove the chosen character from
**str**. - Repeat the above steps while there are characters left in str.

The task is to generate **X** such that it is lexicographically smallest possible then print the generated string.

**Examples:**

Input:str = “geek”, K = 2Output:eegk

Operation 1: str = “gek”, X = “e”

Operation 2: str = “gk”, X = “ee”

Operation 3: str = “k”, X = “eeg”

Operation 4: str = “”, X = “eegk”

Input:str = “geeksforgeeks”, K = 5Output:eefggeekkorss

**Approach:** In order to get the lexicographically smallest string, we need to take the minimum character from the first **K** characters every time we choose a character from **str**. To do that, we can put the first **K** characters in a priority_queue (min-heap) and then choose the smallest character and append it to **X**. Then, push the next character in **str** to the priority queue and repeat the process until there are characters left to process.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to return the lexicographically ` `// smallest required string ` `string getSmallestStr(string S, ` `int` `K) `
`{ ` ` ` ` ` `// Initially empty string `
` ` `string X = ` `""` `; `
` ` ` ` `// min heap of characters `
` ` `priority_queue<` `char` `, vector<` `char` `>, greater<` `char` `> > pq; `
` ` ` ` `// Length of the string `
` ` `int` `i, n = S.length(); `
` ` ` ` `// K cannot be greater than `
` ` `// the size of the string `
` ` `K = min(K, n); `
` ` ` ` `// First push the first K characters `
` ` `// into the priority_queue `
` ` `for` `(i = 0; i < K; i++) `
` ` `pq.push(S[i]); `
` ` ` ` `// While there are characters to append `
` ` `while` `(!pq.empty()) { `
` ` ` ` `// Append the top of priority_queue to X `
` ` `X += pq.top(); `
` ` ` ` `// Remove the top element `
` ` `pq.pop(); `
` ` ` ` `// Push only if i is less than `
` ` `// the size of string `
` ` `if` `(i < S.length()) `
` ` `pq.push(S[i]); `
` ` ` ` `i++; `
` ` `} `
` ` ` ` `// Return the generated string `
` ` `return` `X; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `string S = ` `"geeksforgeeks"` `; `
` ` `int` `K = 5; `
` ` ` ` `cout << getSmallestStr(S, K); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java implementation of the approach ` `import` `java.util.PriorityQueue; `
` ` `class` `GFG `
`{ ` ` ` ` ` `// Function to return the lexicographically `
` ` `// smallest required string `
` ` `static` `String getSmallestStr(String S, ` `int` `K) `
` ` `{ `
` ` ` ` `// Initially empty string `
` ` `String X = ` `""` `; `
` ` ` ` `// min heap of characters `
` ` `PriorityQueue<Character> pq = ` `new` `PriorityQueue<>(); `
` ` ` ` `// Length of the string `
` ` `int` `i, n = S.length(); `
` ` ` ` `// K cannot be greater than `
` ` `// the size of the string `
` ` `K = Math.min(K, n); `
` ` ` ` `// First push the first K characters `
` ` `// into the priority_queue `
` ` `for` `(i = ` `0` `; i < K; i++) `
` ` `pq.add(S.charAt(i)); `
` ` ` ` `// While there are characters to append `
` ` `while` `(!pq.isEmpty()) `
` ` `{ `
` ` ` ` `// Append the top of priority_queue to X `
` ` `X += pq.peek(); `
` ` ` ` `// Remove the top element `
` ` `pq.remove(); `
` ` ` ` `// Push only if i is less than `
` ` `// the size of string `
` ` `if` `(i < S.length()) `
` ` `pq.add(S.charAt(i)); `
` ` ` ` `i++; `
` ` `} `
` ` ` ` `// Return the generated string `
` ` `return` `X; `
` ` `} `
` ` ` ` `// Driver Code `
` ` `public` `static` `void` `main(String[] args) `
` ` `{ `
` ` `String S = ` `"geeksforgeeks"` `; `
` ` `int` `K = ` `5` `; `
` ` `System.out.println(getSmallestStr(S, K)); `
` ` `} `
`} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

**Output:**

eefggeekkorss

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.

## Recommended Posts:

- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically largest string possible for a given cost of appending characters
- Lexicographically smallest string formed by removing at most one character
- Minimal moves to form a string by adding characters or appending string itself
- Lexicographically smallest string formed by removing duplicates
- Lexicographically largest string formed from the characters in range L and R
- Find a string such that every character is lexicographically greater than its immediate next character
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Lexicographically smallest subsequence possible by removing a character from given string
- Lexicographically smallest string possible by inserting given character
- Swap all occurrences of two characters to get lexicographically smallest string
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Count of times second string can be formed from the characters of first string
- Check whether second string can be formed from characters of first string
- Make lexicographically smallest palindrome by substituting missing characters
- Check if a string can be obtained by appending subsequences of another string
- Check if string S2 can be obtained by appending subsequences of string S1

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.