Skip to content

Instantly share code, notes, and snippets.

@the-undefined
Created February 24, 2016 07:39
Show Gist options
  • Save the-undefined/4b89aedc0fdb9acf22fa to your computer and use it in GitHub Desktop.
Save the-undefined/4b89aedc0fdb9acf22fa to your computer and use it in GitHub Desktop.
All of the possible combinations of letters in a string
def char_combinations(s)
length = s.size
list_comp = Array.new(length) do |start_index|
length.times.inject([]) do |combinations, end_index|
sub_str = s[start_index..end_index]
sub_str.size > 0 ? combinations << sub_str : combinations
end
end.flatten
end
char_combinations('Joe') # => ["J", "Jo", "Joe", "o", "oe", "e"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment