Nielsen interview question

DP question and regex expression