Solution

This is a classic "collision-detection" problem that involves calculating a difference from the final state.

Let M be the number of items in a string. Kasumi Sort can be achieved by taking the minimum number of empty slots for every possible concatenation from an index 00 to NMN-M by the runtime complexity of O(N)O(N)

Side note

Kasumi just likes messing things up. You'd better NOT to reproduce this sorting algorithm at your neighbor supermarket.

Example submission code

C++