Simulate a queue where boys swap with girls one position at a time over several seconds.
Problem
A school queue contains only boys and girls. Each second, every boy who is immediately before a girl swaps places with that girl. All such swaps happen simultaneously in one second.
Given the initial queue and the number of seconds that pass, determine the final arrangement of the queue.
Details
- The queue is represented by a string of characters, where one character stands for a boy and the other for a girl.
- During one second, look at all adjacent pairs from left to right.
- If a boy is directly in front of a girl, they swap positions.
- A person can be involved in at most one swap per second.
Your task is to output the queue after the given number of seconds.
Input Format
Input
- The first line contains two integers and — the queue length and the number of seconds.
- The second line contains a string of length describing the queue.
Output Format
Output
- Print the queue after seconds.
Constraints
Constraints
- The string length is
- The string contains only two distinct characters representing boys and girls
Example 1
Input
5 1 BGGBG
Output
GBGGB
Explanation
In the first second, the boy at position 1 swaps with the girl at position 2, and the boy at position 4 swaps with the girl at position 5.
Example 2
Input
5 2 BGGBG
Output
GGBGB
Explanation
After the first second the queue becomes GBGGB. In the second second, the boy at position 2 swaps with the girl at position 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.