// Welcome to the TypeScript Playground, this is a website
// which gives you a chance to write, share and learn TypeScript.
//@ts-ignore
type U<N,A=[]>=A extends{length:N}?A:U<N,[...A,1]>;type F<C="",S=[],V=S extends[{},{},...infer R]?R:S>=C extends`${infer I} ${infer R}`?F<R,I extends`${infer I extends number}`?[U<I>,...S]:{p:[S[1],...V],d:[S[0],...S],x:[S[1],S[0],...V]a:[[...S[0],...S[1]],...V],s:S[1]extends[...S[0],...infer Z]?[Z,...V]:V}[I]>:S
type z = F<"1 2 a 2 s 5 6 x a ">
// ^?
// stack shown top-to-bottom