How can I interleave or create unique permutations of two strings (without recursion)

后端 未结 4 1144
难免孤独
难免孤独 2020-11-30 08:27

The question is to print all possible interleavings of two given strings. So I wrote a working code in Python which runs like this:

def inter(arr1,arr2,p1,p2         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 08:48

    Does permutations work for you? Or, is this coding practice?

    >>> from itertools import permutations
    >>> s1 = "ab"
    >>> s2 = "cd"
    >>> all_values = [c for c in s1 + s2]
    >>> ["".join(r) for r in permutations(all_values)]
    ['abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', 'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca', 'cabd', 'cadb', 'cbad', 'cbda', 'cdab', 'cdba', 'dabc', 'dacb', 'dbac', 'dbca', 'dcab', 'dcba']
    

提交回复
热议问题