Created
August 2, 2020 15:24
-
-
Save mbiamont/d8b72b0f6ab7513fb8ce71292fbbfa9e to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Call [block] lambda for each subset from the longest to the shortest. | |
*/ | |
inline fun String.forEachSubset(block: (String) -> Unit) { | |
for (i in length downTo 1) { | |
for (j in 0 until length + 1 - i) { | |
block(substring(j, i + j)) | |
} | |
} | |
} | |
/*Example*/ | |
fun main(){ | |
"Kotlin".forEachSubset { | |
println(it) | |
} | |
} | |
// Kotlin | |
// Kotli | |
// otlin | |
// Kotl | |
// otli | |
// tlin | |
// Kot | |
// otl | |
// tli | |
// lin | |
// Ko | |
// ot | |
// tl | |
// li | |
// in | |
// K | |
// o | |
// t | |
// l | |
// i | |
// n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment