Skip to content

Instantly share code, notes, and snippets.

@yanks
Created June 2, 2011 04:33
Show Gist options
  • Save yanks/1003937 to your computer and use it in GitHub Desktop.
Save yanks/1003937 to your computer and use it in GitHub Desktop.
finding anagrams
require 'set'
str = String.new(ARGV[0])
if str.nil? || str.length < 2 then
puts "usage: anagram <string>"
exit
end
anagram_set = SortedSet.new
(0..str.length-1).each do
(0..str.length-1).each do |i|
str_builder = str[i..i]
anagram_set.add(str_builder)
(0..str.length-1).each do |j|
if i != j then
str_builder += str[j..j]
if !anagram_set.member?(str_builder) then
anagram_set.add(str_builder)
end
end
end
end
#shift
str = str[str.length-1..str.length-1] + str.chop
end
puts anagram_set.to_a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment