Created
February 24, 2016 07:39
-
-
Save the-undefined/4b89aedc0fdb9acf22fa to your computer and use it in GitHub Desktop.
All of the possible combinations of letters in 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
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