Finding all combinations of well-formed brackets

后端 未结 29 1627
盖世英雄少女心
盖世英雄少女心 2020-11-28 02:34

This came up while talking to a friend and I thought I\'d ask here since it\'s an interesting problem and would like to see other people\'s solutions.

The task is to

29条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 02:55

    def form_brackets(n: int) -> set:
        combinations = set()
        if n == 1:
            combinations.add('()')
        else:
            previous_sets = form_brackets(n - 1)
            for previous_set in previous_sets:
                for i, c in enumerate(previous_set):
                    temp_string = "{}(){}".format(previous_set[:i+1], previous_set[i+1:])
                    combinations.add(temp_string)
    
        return combinations
    

提交回复
热议问题