itertools.permutations()

  • + 0 comments
    from itertools import permutations
    
    # Input the string and integer k
    input_data = input().split()
    S = input_data[0]
    k = int(input_data[1])
    
    # Generate and sort permutations
    result = permutations(sorted(S), k)
    
    # Print each permutation
    for perm in result:
        print(''.join(perm))