import java.math.BigInteger
import java.text.NumberFormat

fun main() {
    for (s in arrayOf(
        "0",
        "9",
        "12",
        "21",
        "12453",
        "738440",
        "45072010",
        "95322020",
        "9589776899767587796600",
        "3345333"
    )) {
        println("${format(s)} -> ${format(next(s))}")
    }
    testAll("12345")
    testAll("11122")
}

private val FORMAT = NumberFormat.getNumberInstance()
private fun format(s: String): String {
    return FORMAT.format(BigInteger(s))
}

private fun testAll(str: String) {
    var s = str
    println("Test all permutations of:  $s")
    val sOrig = s
    var sPrev = s
    var count = 1