Created
January 28, 2022 07:06
-
-
Save jollyjoester/95d0a08fd870f7683d2c7bf8ed2a695b to your computer and use it in GitHub Desktop.
What is the 100th word in the permutation of "GAKKOU"? in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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