Skip to content

Instantly share code, notes, and snippets.

@jollyjoester
Created January 28, 2022 07:06
Show Gist options
  • Save jollyjoester/95d0a08fd870f7683d2c7bf8ed2a695b to your computer and use it in GitHub Desktop.
Save jollyjoester/95d0a08fd870f7683d2c7bf8ed2a695b to your computer and use it in GitHub Desktop.
What is the 100th word in the permutation of "GAKKOU"? in Kotlin
fun main() {
val string = "GAKKOU"
val chars: List<Char> = string.toList()
val word = chars.permutations().distinct().map { it.toCharArray().concatToString() }.sorted()[99]
println(word)
}
fun <T> List<T>.permutations(): List<List<T>> {
if (this.isEmpty()) return listOf(emptyList())
val result: MutableList<List<T>> = mutableListOf()
for (i in this.indices) {
(this - this[i]).permutations().forEach {
item -> result.add(item + this[i])
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment