MRSOOL interview question

Simple/medium problem from leet code