Created
June 2, 2011 04:33
-
-
Save yanks/1003937 to your computer and use it in GitHub Desktop.
finding anagrams
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
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