itertools.combinations()

  • + 0 comments

    Solution without nested for loops (better time complexity)

    from itertools import combinations, chain
    S = input().split()
    n = int(S[1])
    
    combs = chain.from_iterable(combinations(sorted(S[0]), i) for i in range(1, n + 1))
    for comb in combs:
        print(''.join(comb))