Help required in problem from CSES

How to solve this problem → CSES - Word Combinations