Adding permutations logic in Scala.