Solve #98 in scala