itertools.combinations()

  • + 0 comments

    from itertools import combinations

    Read input

    S, k = input().split() k = int(k)

    Sort the string to ensure lexicographic order

    S = ''.join(sorted(S))

    Generate and print combinations for each length from 1 to k

    for i in range(1, k + 1): for combo in combinations(S, i): print(''.join(combo))