Created
September 15, 2013 14:39
-
-
Save DiveInto/6571329 to your computer and use it in GitHub Desktop.
compute all permutations of a string
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
object per extends App{ | |
def permutations(str: String):List[String] = { | |
if(str.size == 0) | |
List("") | |
else{ | |
val res = { | |
for{c <- str | |
per <- permutations(str.replaceFirst(c.toString, "")) | |
}yield c + per | |
} | |
▫▫ | |
res.toList | |
} | |
} | |
val res = permutations("abc") | |
res.foreach(println) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment