Solve #121 in scala