Solve #26 in scala