Skip to content

Instantly share code, notes, and snippets.

@simpleauthority
Created May 30, 2019 03:48
Show Gist options
  • Save simpleauthority/68aa0b00aa2e946a9dde8a9f0f11728c to your computer and use it in GitHub Desktop.
Save simpleauthority/68aa0b00aa2e946a9dde8a9f0f11728c to your computer and use it in GitHub Desktop.
Fill in the given format and any unknowns with a * and the program will replace the *s with possible variations. Read through the output to try and figure out the string that you forgot.
fun main() {
// Type what you know here, and replace unknowns with the star symbol (*). The more star symbols you have
// the more permutations you will have. This increases exponentially.
val givenFormat = "myemailwithm**singcha*s@gmail.com"
// DO NOT CHANGE ANYTHING BEYOND THIS LINE
val generator = PermutationGenerator(givenFormat)
generator.generate()
}
class PermutationGenerator(
private val givenFormat: String
) {
private val unknownIndices = HashSet<Int>()
init {
givenFormat.toCharArray().forEachIndexed { idx, c ->
if (c == '*') {
unknownIndices.add(idx)
}
}
}
fun generate() {
println("Generating permutations...")
generate(unknownIndices.size, "")
}
private fun generate(length: Int, current: String) {
if (current.length < length) {
var c = 'A'
while (c <= 'z') {
generate(length, current + c)
c++
}
} else {
var idx = 0
var newStr = givenFormat
unknownIndices.forEach {
val chars = newStr.toCharArray()
chars[it] = current[idx]
newStr = String(chars)
idx++
}
println("Email permutation: $newStr")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment