Optiver Interview Question

Tricky DFS question involving words