(Python)
Problem Statement:
You are given a string S.
Your task is to print all possible combinations, up to size, of the string in lexicographically sorted order.
Input Format
A single line containing the string and integer value separated by a space.
Constraints
0 < k <= len(S)
The string contains only UPPERCASE characters.
Output Format
Print the different combinations of the string on separate lines.
Sample Input
HACK 2
Sample Output
ACHKACAHAKCHCKHK
Solution:
from itertools import combinations# Input from userinp = input().split()S = inp[0]k = int(inp[1])li1 = []# Create a list with combinationsfor i in range(1, k+1): li1.extend(list(combinations(S, i)))# Sort the list lexicographicallyfor i in range(0, len(li1)): li1[i] = str(''.join(sorted(list(li1[i]))))li1 = sorted(li1, key=lambda s: (len(s), s.lower())) # sort the list alphabetically ascending# print the stringfor i in range(0, len(li1)): print(li1[i])
Output:

Reference:
