Skip to content

Instantly share code, notes, and snippets.

@mbiamont
Created August 2, 2020 15:24
Show Gist options
  • Save mbiamont/d8b72b0f6ab7513fb8ce71292fbbfa9e to your computer and use it in GitHub Desktop.
Save mbiamont/d8b72b0f6ab7513fb8ce71292fbbfa9e to your computer and use it in GitHub Desktop.
/**
* 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