Skip to content

Instantly share code, notes, and snippets.

@josnidhin
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josnidhin/7dfd03b236050ee660c4 to your computer and use it in GitHub Desktop.
Save josnidhin/7dfd03b236050ee660c4 to your computer and use it in GitHub Desktop.
Prints all the permutation of the given string
def permutation(prefix, str)
n = str.length
if n == 0
p prefix
else
for i in 0..n-1 do
if i == 0
perm("#{prefix}#{str[i]}", str[i+1..n-1])
else
perm("#{prefix}#{str[i]}", "#{str[0..i-1]}#{str[i+1..n-1]}")
end
end
end
end
permutation('', 'abc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment