Solve #99 in scala