Nielsen Interview Question

DP question and regex expression