Bob and the minimum string
Practice
4.3 (6 votes)
Basics of greedy algorithms
Greedy algorithms
Algorithms
Problem
54% Success 520 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Bob has been provided a string \(S\) of size \(N\) containing lowercase characters. He took two empty strings, \(U\) and \(V\), and performed the following operations:

  1. Extract the first character from string \(S\) and append it at the end of string \(U\).
  2. Extract the last character from string \(U\) and append it at the end of string \(V\).

Bob can perform the above operations any number of times. After some operations, he wants to get the string \(S\) and \(U\) empty, and string \(V\) should be lexicographically minimum. 

Your task is to help Bob find the lexicographically minimum possible string \(V\) that can be formed.

Input Format:

  • The first line of input contains an integer \(T\), denoting the number of test cases.
  • For each test case, the first line will contain integer \(N\), the size of the input string \(S\), and the second line will contain the string \(S\) itself.

Output format:

For each test case, print the lexicographically minimum possible string \(V\) that can be formed.

Constraints:

\(1 <= T <= 5\)

\(1 <= N <= 10^5\)

\(S[i]\) will be a lowercase english character.

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:30
33 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsGreedy Algorithms
Points:30
4 votes
Tags:
Brute ForceGreedy AlgorithmsBasics of Greedy AlgorithmsAlgorithms
Points:30
4 votes
Tags:
Greedy algorithmAlgorithmsArraysGreedy AlgorithmsBasics of Greedy Algorithms