Jane Street Interview Question

String question. Must have missed an optimisation .