# Lexicographically smallest string formed by appending a character from the first K characters of a given string

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

`// C++ program to find the new string ` `// after performing deletions and append ` `// operation in the string s ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the new string thus ` `// formed by removing characters ` `string newString(string s, ` `int` `k) ` `{ ` ` ` `// new string ` ` ` `string X = ` `""` `; ` ` ` ` ` `// Remove characters until ` ` ` `// the string is empty ` ` ` `while` `(s.length() > 0) { ` ` ` ` ` `char` `temp = s[0]; ` ` ` ` ` `// Traverse to find the smallest character in the ` ` ` `// first k characters ` ` ` `for` `(` `long` `long` `i = 1; i < k and i < s.length(); i++) { ` ` ` `if` `(s[i] < temp) { ` ` ` `temp = s[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// append the smallest character ` ` ` `X = X + temp; ` ` ` ` ` `// removing the lexicographically smallest ` ` ` `// character from the string ` ` ` `for` `(` `long` `long` `i = 0; i < k; i++) { ` ` ` `if` `(s[i] == temp) { ` ` ` ` ` `s.erase(s.begin() + i); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `X; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `string s = ` `"gaurang"` `; ` ` ` `int` `k = 3; ` ` ` ` ` `cout << newString(s, k); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the new string ` `// after performing deletions and append ` `// operation in the string s ` ` ` `class` `GFG { ` ` ` `// Function to find the new string thus ` `// formed by removing characters ` ` ` `static` `String newString(String s, ` `int` `k) { ` ` ` `// new string ` ` ` `String X = ` `""` `; ` ` ` ` ` `// Remove characters until ` ` ` `// the string is empty ` ` ` `while` `(s.length() > ` `0` `) { ` ` ` ` ` `char` `temp = s.charAt(` `0` `); ` ` ` ` ` `// Traverse to find the smallest character in the ` ` ` `// first k characters ` ` ` `for` `(` `int` `i = ` `1` `; i < k && i < s.length(); i++) { ` ` ` `if` `(s.charAt(i) < temp) { ` ` ` `temp = s.charAt(i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// append the smallest character ` ` ` `X = X + temp; ` ` ` ` ` `// removing the lexicographically smallest ` ` ` `// character from the string ` ` ` `for` `(` `int` `i = ` `0` `; i < k; i++) { ` ` ` `if` `(s.charAt(i) == temp) { ` ` ` ` ` `s = s.substring(` `0` `, i) + s.substring(i + ` `1` `); ` ` ` `//s.erase(s.begin() + i); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `X; ` ` ` `} ` `// Driver code ` ` ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `String s = ` `"gaurang"` `; ` ` ` `int` `k = ` `3` `; ` ` ` ` ` `System.out.println(newString(s, k)); ` ` ` ` ` `} ` `} ` ` ` `// This code contributed by Jajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

# Python 3 program to find the new string

# after performing deletions and append

# operation in the string s

# Function to find the new string thus

# formed by removing characters

def newString(s, k):

# new string

X = “”

# Remove characters until

# the string is empty

while (len(s) > 0):

temp = s[0]

# Traverse to find the smallest

# character in the first k characters

i = 1

while(i < k and i < len(s)):
if (s[i] < temp):
temp = s[i]
i += 1
# append the smallest character
X = X + temp
# removing the lexicographically
# smallest character from the string
for i in range(k):
if (s[i] == temp):
s = s[0:i] + s[i + 1:]
break
return X
# Driver Code
if __name__ == '__main__':
s = "gaurang"
k = 3
print(newString(s, k))
# This code is contributed by
# Shashank_Sharma
[tabby title="C#"]

`// C# program to find the new string ` `// after performing deletions and ` `// append operation in the string s ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the new string thus ` `// formed by removing characters ` `static` `String newString(String s, ` `int` `k) ` `{ ` ` ` `// new string ` ` ` `String X = ` `""` `; ` ` ` ` ` `// Remove characters until ` ` ` `// the string is empty ` ` ` `while` `(s.Length > 0) ` ` ` `{ ` ` ` `char` `temp = s[0]; ` ` ` ` ` `// Traverse to find the smallest ` ` ` `// character in the first k characters ` ` ` `for` `(` `int` `i = 1; i < k && i < s.Length; i++) ` ` ` `{ ` ` ` `if` `(s[i] < temp) ` ` ` `{ ` ` ` `temp = s[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// append the smallest character ` ` ` `X = X + temp; ` ` ` ` ` `// removing the lexicographically smallest ` ` ` `// character from the string ` ` ` `for` `(` `int` `i = 0; i < k; i++) ` ` ` `{ ` ` ` `if` `(s[i] == temp) ` ` ` `{ ` ` ` ` ` `s = s.Substring(0, i) + s.Substring(i + 1); ` ` ` `//s.erase(s.begin() + i); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `X; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String s = ` `"gaurang"` `; ` ` ` `int` `k = 3; ` ` ` ` ` `Console.Write(newString(s, k)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

agangru

## Recommended Posts:

- Lexicographically smallest string formed by removing at most one character
- Lexicographically largest string formed from the characters in range L and R
- Minimal moves to form a string by adding characters or appending string itself
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Check whether second string can be formed from characters of first string
- Find a string such that every character is lexicographically greater than its immediate next character
- Find the smallest window in a string containing all characters of another string
- Lexicographically next greater string using same character set
- Lexicographically smallest permutation of a string with given subsequences
- K-th lexicographically smallest unique substring of a given string
- Count of strings that can be formed from another string using each character at-most once
- Lexicographically smallest string obtained after concatenating array
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Maximum number of characters between any two same character in a string

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.