Optiver interview question

Tricky DFS question involving words