Search code examples
pythonstringcombinationspermutationdiscrete-mathematics

Getting All Combinations or Permutations of a Character using terms


i want all combinations words of character using terms. Example :

word = 'aan'
result = ['ana', 'naa', 'aan']

terms :

number of character 'a' -> 2
number of character 'n' -> 1


Solution

  • I tried a one liner solution, and give the result in a list

    You can use permutation tools from itertools package to get all of the permutations (not combinations) solutions

    from itertools import permutations
    word = 'aan'
    list(set([ ''.join(list(i)) for i in permutations(word,len(word))]))