Optimization: used a Set instead of Seq to search for solutions