String Transformation Problem Statement

Given a string str of length N, perform a series of operations to create a new string:

  1. Select the smallest character from the first 'K' characters of the string, remove it from the string and append it to the new string.
  2. Continue this process until the original string is empty.

If fewer than 'K' characters remain, sort these characters and append them in order to the new string.

Input:

The first line contains an integer 'T' denoting the number of test cases.
For each test case, the input contains a single string and an integer 'K' separated by a space.

Output:

For each test case, output the new string formed after performing the operations. Each output should be on a new line.

Example:

Input:
2
edcba 4
abcde 2
Output:
bacde
abcde

Constraints:

  • 1 <= T <= 100
  • 0 < N <= 10^5
  • 1 <= K <= 10^5
  • Time Limit: 1 second

Note:

The string contains only lowercase letters and no spaces. Do not print from your function; use return values instead.
AnswerBot
6d

The problem involves selecting the smallest character from the first 'K' characters of a string and appending it to a new string until the original string is empty.

  • Iterate through the string, selectin...read more

Help your peers!
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter