fun main() {
val words = listOf("Kotlin", "Programming", "Language")
val permutations = mutableListOf<List<String>>()
generatePermutations(words, mutableListOf(), permutations)
permutations.forEach { println(it) }
}
fun generatePermutations(words: List<String>, current: MutableList<String>, permutations: MutableList<List<String>>) {
if (current.size == words.size) {
permutations.add(current.toList())
return
}
for (word in words) {
if (!current.contains(word)) {
current.add(word)
generatePermutations(words, current, permutations)
current.removeAt(current.size - 1)
}
}
}
/*
run:
[Kotlin, Programming, Language]
[Kotlin, Language, Programming]
[Programming, Kotlin, Language]
[Programming, Language, Kotlin]
[Language, Kotlin, Programming]
[Language, Programming, Kotlin]
*/