Getting all combinations of a string and its substrings

后端 未结 4 1748
心在旅途
心在旅途 2020-12-03 05:32

I\'ve seen many questions on getting all the possible substrings (i.e., adjacent sets of characters), but none on generating all possible strings including the combinations

4条回答
  •  萌比男神i
    2020-12-03 05:56

    def return_substrings(s):
        all_sub = set()
        recent = {s}
    
        while recent:
            tmp = set()
            for word in recent:
                for i in range(len(word)):
                    tmp.add(word[:i] + word[i + 1:])
            all_sub.update(recent)
            recent = tmp
    
        return all_sub
    

提交回复
热议问题