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 the smallest possible then print the generated string. Examples:
Input: str = “geek”, K = 2
Output: 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 = 5
Output: 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++
#include <bits/stdc++.h>
using namespace std;
string getSmallestStr(string S, int K)
{
string X = "" ;
priority_queue< char , vector< char >, greater< char > > pq;
int i, n = S.length();
K = min(K, n);
for (i = 0; i < K; i++)
pq.push(S[i]);
while (!pq.empty()) {
X += pq.top();
pq.pop();
if (i < S.length())
pq.push(S[i]);
i++;
}
return X;
}
int main()
{
string S = "geeksforgeeks" ;
int K = 5;
cout << getSmallestStr(S, K);
return 0;
}
|
Java
import java.util.PriorityQueue;
class GFG
{
static String getSmallestStr(String S, int K)
{
String X = "" ;
PriorityQueue<Character> pq = new PriorityQueue<>();
int i, n = S.length();
K = Math.min(K, n);
for (i = 0 ; i < K; i++)
pq.add(S.charAt(i));
while (!pq.isEmpty())
{
X += pq.peek();
pq.remove();
if (i < S.length())
pq.add(S.charAt(i));
i++;
}
return X;
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
int K = 5 ;
System.out.println(getSmallestStr(S, K));
}
}
|
C#
using System;
using System.Collections.Generic;
namespace GetSmallestString
{
class Program
{
static string GetSmallestStr( string S, int K)
{
string X = "" ;
var pq = new SortedSet< char >();
int i, n = S.Length;
K = Math.Min(K, n);
for (i = 0; i < K; i++)
pq.Add(S[i]);
while (pq.Count > 0)
{
X += pq.Min;
pq.Remove(pq.Min);
if (i < S.Length)
pq.Add(S[i]);
i++;
}
return X;
}
static void Main( string [] args)
{
string S = "geeksforgeeks" ;
int K = 5;
Console.WriteLine(GetSmallestStr(S, K));
}
}
}
|
Javascript
class GFG {
static getSmallestStr(S, K) {
let X = "" ;
let pq = [];
let i, n = S.length;
K = Math.min(K, n);
for (i = 0; i < K; i++) {
pq.push(S.charAt(i));
}
pq.sort();
while (pq.length > 0) {
X += pq[0];
pq.shift();
if (i < S.length) {
pq.push(S.charAt(i));
}
i++;
pq.sort();
}
return X;
}
static main() {
let S = "geeksforgeeks" ;
let K = 5;
console.log(GFG.getSmallestStr(S, K));
}
}
GFG.main();
|
Python3
import heapq
def get_smallest_str(s: str , k: int ) - > str :
x = ""
pq = []
n = len (s)
k = min (k, n)
for i in range (k):
heapq.heappush(pq, s[i])
i = k
while pq:
x + = heapq.heappop(pq)
if i < n:
heapq.heappush(pq, s[i])
i + = 1
return x
s = "geeksforgeeks"
k = 5
print (get_smallest_str(s, k))
|
Time Complexity: O(nlogn) where n is the length of the string.
Auxiliary Space: O(K), as extra space of size K is used to build priorityQueue