All ways to partition a string

后端 未结 5 1963
深忆病人
深忆病人 2021-01-01 00:08

I\'m trying to find a efficient algorithm to get all ways to partition a string

eg for a given string \'abcd\' =>
\'a\' \'bcd\'
\'a\' \'b\' \'cd\'
\'a\'

5条回答
  •  借酒劲吻你
    2021-01-01 00:37

    I just wanted to post a simple recursive solution to this problem for anyone stumbling on this question. Probably not the best way, but this was way simpler for me to understand and implement. If I am wrong, please correct me.

    def party(s:str, P:list, res:list) -> None :
        """Recursively generates all partitions of a given string"""
        res.append(P+[s])
        for i in range(1,len(s)):
            party(s[i:],P+[s[:i]],res)
    
    res = []
    party("abcd",[],res)
    print(res)
    """
    [['abcd'], ['a', 'bcd'], ['a', 'b', 'cd'], ['a', 'b', 'c', 'd'], 
    ['a', 'bc', 'd'], ['ab', 'cd'], ['ab', 'c', 'd'], ['abc', 'd']]
    """
    

    It works as follows: Given a string or a substring of it, we can split after each of its character creating two halves. Say: "abc" can be partitioned into ["a","bc"], ["ab","c"]

    We save the first part in a intermediate partition P and recursively call party on the other half.

    Because both halves together form a complete partition we save it to res. Example:

    initially: s = "abc" is a valid partition, save it to res.

    recr call: s = "bc", P = ["a"] , so P +[s]= ["a","bc"] is also valid, save it to res.

    Proceed with splitting "bc". P = ["a","b"], s="c" so P + [s] is also valid. And so on..

    recr call 3: s = "c", P = ["ab"], so P + [s] =["ab","c"] is also valid, save it to res

    Working:

    tests = ["abc","abcd","a"]
    for t in tests:
        res = []
        party(t,[],res)
        print(f'{t} -> {res} \n')
    
    """Output
    abc -> [['abc'], ['a', 'bc'], ['a', 'b', 'c'], ['ab', 'c']] 
    
    abcd -> [['abcd'], ['a', 'bcd'], ['a', 'b', 'cd'], ['a', 'b', 'c', 'd'], 
    ['a', 'bc', 'd'], ['ab', 'cd'], ['ab', 'c', 'd'], ['abc', 'd']] 
    
    a -> [['a']] 
    """
    

提交回复
热议问题