An automated sorting line carries crates, each stamped with a single lowercase letter, past a re-orientation station in a fixed left-to-right order. The station processes the belt in consecutive batches of 2k crates: for each batch, a robotic arm grips the first k crates and flips their order end-to-end, then lets the remaining crates in that batch roll through untouched. Given the full sequence of letters on the belt and the batch size parameter k, determine the final left-to-right order of letters after every batch has been processed. A trailing batch with fewer than k crates has all of its crates flipped; a trailing batch with between k and 2k crates has exactly its first k crates flipped and the rest left as-is.
s of lowercase English letters — the crate sequence (1 ≤ |s| ≤ 10000).k (1 ≤ k ≤ 10000).The resulting string after processing every batch, on one line.
Example 1
Input
abcdefg 2
Expected
bacdfeg
Explanation
With k=2 the batch size is 4. The first batch "abcd" has its first 2 letters reversed ("ab"→"ba") and the last 2 left alone, giving "bacd". Only 3 letters remain ("efg"), which is at least k=2, so the first 2 ("ef"→"fe") are reversed and the last 1 ("g") is left alone, giving "feg". The result is "bacd"+"feg" = "bacdfeg".
Example 2
Input
abcd 2
Expected
bacd
Explanation
The whole string is exactly one batch of 2k=4 letters. The first k=2 letters "ab" are reversed to "ba", and the last 2 letters "cd" are left untouched, giving "bacd".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →