Solve #97 in scala