Solve #118 in scala