Karthik Selvakumar Bhuvaneswaran is a Software Engineer working on Saas(RoR) and PaaS(Salesforce.com) applications. He beleives on day starts at night and requires nothing more than Music, a cup of cofee and a fully charged laptop.
© 2016 karthikselva. Distributed with an MIT license.
def get_first(str) str[0..0] end def exclude_first(str) str[1..-1] end def subsets(set) if set.length > 0 all_sets = subsets(exclude_first(set)) all_sets.clone.each do |val| all_sets << val+get_first(set) end return all_sets else return [set] end end puts subsets('abcd').sort.inspect #Output: #["", "a", "b", "ba", "c", "ca", "cb", "cba", "d", "da", "db", "dba", "dc", "dca", "dcb", "dcba"]