Lexicographically smallest string formed by appending a character from the first K characters of a given string
Last Updated :
26 Aug, 2022
Given a string S consisting of lowercase alphabets. The task is to find the lexicographically smallest string X of the same length only that can be formed using the operation given below:
In a single operation, select any one character among the at most first K characters of string S, remove it from string S and append it to string X. Apply this operation as many times as he wants.
Examples:
Input: str = “gaurang”, k=3
Output: agangru
Remove ‘a’ in the first step and append to X.
Remove ‘g’ in the second step and append to X.
Remove ‘a’ in the third step and append to X.
Remove ‘n’ in the third step and append to X.
Pick the lexicographically smallest character at every step from the first K characters to get the
string “agangru”
Input: str = “geeksforgeeks”, k=5
Output: eefggeekkorss
Approach:
- Find the smallest character in the first k characters in the string S.
- Delete the smallest character found from the string.
- Append the smallest character found to the new string X.
- Repeat the above steps till the string s is empty.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string newString(string s, int k)
{
string X = "" ;
while (s.length() > 0) {
char temp = s[0];
for ( long long i = 1; i < k and i < s.length(); i++) {
if (s[i] < temp) {
temp = s[i];
}
}
X = X + temp;
for ( long long i = 0; i < k; i++) {
if (s[i] == temp) {
s.erase(s.begin() + i);
break ;
}
}
}
return X;
}
int main()
{
string s = "gaurang" ;
int k = 3;
cout << newString(s, k);
}
|
Java
class GFG {
static String newString(String s, int k) {
String X = "" ;
while (s.length() > 0 ) {
char temp = s.charAt( 0 );
for ( int i = 1 ; i < k && i < s.length(); i++) {
if (s.charAt(i) < temp) {
temp = s.charAt(i);
}
}
X = X + temp;
for ( int i = 0 ; i < k; i++) {
if (s.charAt(i) == temp) {
s = s.substring( 0 , i) + s.substring(i + 1 );
break ;
}
}
}
return X;
}
public static void main(String[] args) {
String s = "gaurang" ;
int k = 3 ;
System.out.println(newString(s, k));
}
}
|
Python3
def newString(s, k):
X = ""
while ( len (s) > 0 ):
temp = s[ 0 ]
i = 1
while (i < k and i < len (s)):
if (s[i] < temp):
temp = s[i]
i + = 1
X = X + temp
for i in range (k):
if (s[i] = = temp):
s = s[ 0 :i] + s[i + 1 :]
break
return X
if __name__ = = '__main__' :
s = "gaurang"
k = 3
print (newString(s, k))
|
C#
using System;
class GFG
{
static String newString(String s, int k)
{
String X = "" ;
while (s.Length > 0)
{
char temp = s[0];
for ( int i = 1; i < k && i < s.Length; i++)
{
if (s[i] < temp)
{
temp = s[i];
}
}
X = X + temp;
for ( int i = 0; i < k; i++)
{
if (s[i] == temp)
{
s = s.Substring(0, i) + s.Substring(i + 1);
break ;
}
}
}
return X;
}
public static void Main(String[] args)
{
String s = "gaurang" ;
int k = 3;
Console.Write(newString(s, k));
}
}
|
PHP
<?php
function newString( $s , $k )
{
$X = "" ;
while ( strlen ( $s ) > 0)
{
$temp = $s [0];
for ( $i = 1; $i < $k &&
$i < strlen ( $s ); $i ++)
{
if ( $s [ $i ] < $temp )
{
$temp = $s [ $i ];
}
}
$X = $X . $temp ;
for ( $i = 0; $i < $k ; $i ++)
{
if ( $s [ $i ] == $temp )
{
$s = substr ( $s , 0, $i ) .
substr ( $s , $i + 1, strlen ( $s ));
break ;
}
}
}
return $X ;
}
$s = "gaurang" ;
$k = 3;
echo (newString( $s , $k ));
?>
|
Javascript
<script>
function newString(s, k) {
var X = "" ;
while (s.length > 0) {
var temp = s[0];
for ( var i = 1; i < k && i < s.length; i++)
{
if (s[i] < temp) {
temp = s[i];
}
}
X = X + temp;
for ( var i = 0; i < k; i++) {
if (s[i] === temp) {
s = s.substring(0, i) + s.substring(i + 1);
break ;
}
}
}
return X;
}
var s = "gaurang" ;
var k = 3;
document.write(newString(s, k));
</script>
|
Complexity Analysis:
- Time Complexity: O(n*n), as nested loops are used
- Auxiliary Space: O(1), as no extra space is used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...