String Challenge
Practice
2.4 (5 votes)
String algorithms
Algorithms
String searching
Problem
83% Success 3564 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given a string S consisting of lower case English alphabets and an integer K.

You need to form a new string P that follows the following conditions:

  • Length of P should be equal to K + length of string S.
  • P should only be formed by using the prefixes of string S.
  • P should be lexicographically smallest.

Task

Determine the string P that satisfies all the given conditions.

Example

Assumptions

  • N = 3
  • K = 4
  • S = bca

Approach

  • Prefixes of S are "b", "bc", "bca".
  • String P will be "bbbbbbb" of length 7 (N + K i.e. 3 + 4).

Function Description

Complete the solve function provided in the editor. This function takes the following 3 parameters and returns the valid string:

  • N: Represents the size of string S.
  • K: Represents the given integer.
  • S: Represents the given string.

Input Format

Note: This is the input format you must use to provide custom input (available above the Compile and Test button).

  • The first line contains a single integer T denoting the number of test cases.
  • For each test case:
    • The first line contains a single integer N denoting the size of string S.
    • The second line contains a single integer K denoting the number of new characters added in string S.
    • The third line contains the string S.

Output Format

For each test case in a separate line, output a string that satisfies all the given conditions.

Constraints

\(1 \le T \le 10 \\ 2 \le N \le 2 * 10^5 \\ 2 \le K \le 2 * 10^5\)

Code snippets (also called starter code/boilerplate code) 

This question has code snippets for C, CPP, Java, and Python.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
9 votes
Tags:
AlgorithmsEasyString Manipulation
Points:20
61 votes
Tags:
EasyImplementationString Manipulation
Points:20
4 votes
Tags:
String SearchingAlgorithmsReal worldString Algorithms