itertools.combinations(iterable, r) 

This tool returns the $\textbf{r}$ length subsequences of elements from the input iterable.

Combinations are emitted in lexicographic sorted order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

 Sample Code 

>>> from itertools import combinations
>>> 
>>> print list(combinations('12345',2))
[('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '4'), ('3', '5'), ('4', '5')]
>>> 
>>> A = [1,1,3,3,3]
>>> print list(combinations(A,4))
[(1, 1, 3, 3), (1, 1, 3, 3), (1, 1, 3, 3), (1, 3, 3, 3), (1, 3, 3, 3)]

Task

You are given a string $\mbox{S}$. 

Your task is to print all possible combinations, up to size $\boldsymbol{\mbox{k}}$, of the string in lexicographic sorted order.

Input Format

A single line containing the string $\mbox{S}$ and integer value $\boldsymbol{\mbox{k}}$ separated by a space.

Constraints

$0<k\leq len(S)$ 

The string contains only UPPERCASE characters.

Output Format

Print the different combinations of string $\mbox{S}$ on separate lines.

Sample Input
HACK 2

Sample Output
A
C
H
K
AC
AH
AK
CH
CK
HK

Difficulty: EASY

Solutions:
from itertools import combinations
(S, l) = input().split(' ')
l = int(l)
S = sorted(S)
for i in range(l):
	C = list(combinations(S, i + 1))
	for c in C:
		print(''.join(c))


from itertools import combinations
(word, num) = input().split()
word = sorted(word)
for i in range(1, int(num) + 1):
	p = combinations(word, i)
	for j in p:
		print(''.join(j))


from itertools import combinations
(string, n) = input().split()
for i in range(1, int(n) + 1):
	for x in list(combinations(sorted(string), int(i))):
		print(''.join([i for i in x]))


(S, k) = input().split()
from itertools import combinations
m = list()
for i in range(1, int(k) + 1):
	Comb = [list(a) for a in combinations(S, i)]
	for a in Comb:
		a.sort()
	m.extend(sorted((''.join(a) for a in Comb)))
for a in m:
	print(a)


from itertools import combinations
(S, k) = input().split(' ')
S = sorted(S)
k = int(k)

def comb(value):
	for i in list(combinations(S, value)):
		print(''.join(i))
for i in range(1, k + 1):
	comb(i)


import itertools
(s, k) = input().split()
for i in range(1, int(k) + 1):
	comb = itertools.combinations(sorted(list(s)), i)
	for item in comb:
		print(''.join(item))


from itertools import combinations
(word, size) = [i for i in input().split(' ')]
size = int(size)
for i in range(size):
	for el in combinations(sorted(word), i + 1):
		print(''.join(el))


from itertools import combinations
inp = input().split()
s = inp[0]
k = int(inp[1])
for i in range(1, k + 1):
	lis1 = list(combinations(s, i))
	lis2 = []
	for x in range(len(lis1)):
		lis2.append(''.join(sorted(lis1[x])))
	lis2 = sorted(lis2)
	for x in range(len(lis2)):
		print(''.join(lis2[x]))


import itertools
(S, k) = input().split()
for m in range(int(k)):
	print('\n'.join((''.join(x) for x in itertools.combinations(sorted(S), m + 1))))


from itertools import combinations
(s, k) = input().split()
k = int(k)
s = sorted(s)
for t in range(1, k + 1):
	for i in combinations(s, t):
		print(''.join(i))


from itertools import combinations
(S, k) = input().split()
for j in range(1, int(k) + 1):
	for i in combinations(sorted(S), j):
		print(''.join(i))


from itertools import combinations
import sys
state = 0
for line in sys.stdin:
	(s, k) = line.strip().split(' ')
	s = ''.join(sorted(s))
	k = int(k)
	for i in range(1, k + 1):
		p = list(combinations(s, i))
		for e in p:
			o = ''
			for j in e:
				o += str(j)
			print(o)


from itertools import combinations
(String, Value) = input().split()
String = sorted(String)
Value = int(Value)
for i in range(1, Value + 1):
	for val in list(combinations(String, i)):
		print(''.join(val))


from itertools import combinations as C
line = input().split()
s = list(line[0])
s.sort()
K = int(line[1])
for k in range(1, K + 1):
	a = list(C(s, k))
	for i in a:
		for j in i:
			print(j, end='')
		print()


from itertools import combinations
(s, k) = input().split()
s = ''.join(sorted(s))
for n in range(1, int(k) + 1):
	a = [''.join(p) for p in list(combinations(s, n))]
	for j in a:
		print(j)


from itertools import combinations
(S, k) = input().strip().split()
k = int(k)
P = []
for i in range(1, k + 1):
	P.extend(sorted([''.join(sorted(i)) for i in list(combinations(S, i))]))
print('\n'.join(P))


from itertools import combinations
s = list(map(str, input().split()))
for i in range(1, int(s[1]) + 1):
	L = list(combinations(''.join(sorted(s[0])), i))
	for i in L:
		print(''.join(i))


import itertools
Line = str(input()).split()
S = Line[0]
k = int(Line[1])
Temp1 = []
Temp2 = []
for i in range(1, k + 1):
	Temp1 = []
	List = sorted(list(itertools.combinations(S, i)))
	for Tuple in List:
		String = ''.join(sorted(Tuple))
		Temp1.append(String)
	Temp1 = sorted(Temp1)
	Temp2 += Temp1
for i in range(0, len(Temp2)):
	print(Temp2[i])


from itertools import combinations
imput = input().split()
string = imput[0]
splitString = [c for c in string]
splitString.sort()
max = int(imput[1])
for i in range(max):
	sol = list(combinations(splitString, i + 1))
	for a in sol:
		for e in a:
			print(e, end='')
		print()


from itertools import combinations
(s, k) = input().strip().split()
k = int(k)
for i in range(0, k + 1):
	l = list(combinations(s, i))
	a = list()
	if i > 1:
		for g in l:
			c = list(g)
			c.sort()
			a.append(''.join(c))
	else:
		a = l
	a.sort()
	for g in a:
		if g:
			print(''.join(g))


from itertools import combinations
list1 = input().split()
keylist = list(list1[0])
keylist.sort()
key = str()
for i in range(0, len(keylist)):
	key = key + str(keylist[i])
N = int(list1[1])
comblist = list()
for i in range(1, N + 1):
	templist = list(combinations(key, i))
	comblist.append(templist)
	del templist
for i in range(0, len(comblist)):
	for j in range(0, len(comblist[i])):
		str2 = list()
		str3 = str()
		str1 = str(comblist[i][j])
		str1 = list(str1)
		for k in range(0, len(str1)):
			if str1[k].isupper():
				str2.append(str1[k])
		for q in range(0, len(str2)):
			str3 = str3 + str(str2[q])
		print(str3)
		del str2
		del str3


from itertools import combinations
(s, k) = input().split()
for i in range(1, int(k) + 1):
	res = []
	for x in sorted(list(combinations(s, int(i)))):
		res.append(''.join(sorted(x)))
	print('\n'.join((str(x) for x in sorted(res))))


from itertools import combinations
(S, k) = input().split()
S = sorted(S)
k = int(k)
res = []
for i in range(1, k + 1):
	for thing in list(combinations(S, i)):
		res.append(thing)
for i in res:
	for ch in i:
		print(ch, end='')
	print()


from itertools import combinations
(s, k) = input().split()
l = list(s)
l.sort()
for i in range(1, int(k) + 1):
	print(*map(lambda x: ''.join(x), combinations(''.join(l), i)), sep='\n')


from itertools import combinations
(str, limit) = input().split(' ')
for l in range(1, int(limit) + 1):
	for ele in list(combinations(sorted(str), l)):
		print(''.join(ele))


from itertools import combinations
(S, k) = input().split()
k = int(k)
S = sorted(S)
for n in range(k):
	[print(''.join(i)) for i in list(combinations(S, n + 1))]


import itertools
(a, b) = input().split()
for i in range(1, int(b) + 1):
	for e in itertools.combinations(sorted(a), i):
		print(''.join(e))


from itertools import combinations
val = input().split()
(string, k) = (sorted(list(val[0])), int(val[1]))
for i in range(1, k + 1):
	for j in combinations(string, i):
		print('%s' * len(j) % j)


from itertools import *
(s, k) = input().split()
s = ''.join(sorted(s))
k = int(k)
for i in range(1, k + 1):
	for c in combinations(s, i):
		print(''.join(c))


from itertools import combinations
line = input()
(w, l) = line.split()
l = int(l)
for j in range(1, l + 1):
	for i in combinations(sorted(w), j):
		print(''.join(i))


from itertools import combinations
I = input().split()
s = sorted(I[0])
C = [x for i in range(1, 1 + int(I[1])) for x in combinations(s, i)]
O = [''.join(x) for x in C]
print('\n'.join(O))


from itertools import combinations
(s, k) = input().split()
s = sorted(s)
k = int(k)
for r in range(1, k + 1):
	for i in combinations(s, r):
		print(''.join(i))


from itertools import combinations
(word, num) = input().split()
word = sorted(word)
for m in range(1, int(num) + 1):
	k = combinations(word, m)
	for n in k:
		print(''.join(n))


from itertools import combinations
inputs = input().split()
s = inputs[0]
s = ''.join(sorted(s))
k = int(inputs[1])
for r in range(1, k + 1):
	l = list(combinations(s, r))
	for combination in l:
		for e in combination:
			print(e, end='')
		print()


from itertools import combinations
(S, k) = input().split()
S = sorted(S)
k = int(k)
for i in range(1, k + 1):
	for p in combinations(S, i):
		print(''.join(p))


from itertools import combinations
(s, n) = input().split()
n = int(n)
for i in range(1, n + 1):
	print('\n'.join(map(''.join, combinations(sorted(s), i))))


from itertools import combinations
(N, k) = input().split()
k = int(k)
res = []
for i in range(1, k + 1):
	to_add = list()
	for c in combinations(N, i):
		to_add.append(''.join(sorted(c)))
	res += sorted(to_add)
print('\n'.join(res))


from itertools import combinations
line = input().split()
s = line[0]
r = int(line[1])
s = list(s)
s.sort()
for i in range(1, r + 1):
	lst = list(combinations(s, i))
	if i > 1:
		for j in lst:
			print(str(j).replace('(', '').replace("'", '').replace(')', '').replace(', ', ''))
	else:
		for j in lst:
			print(str(j).replace('(', '').replace("'", '').replace(')', '').replace(',', ''))


from itertools import combinations
(s, n) = input().split()
s = sorted(list(s))
n = int(n)
c = []
i = 1
while i <= n:
	c.extend(combinations(s, i))
	i += 1
for i in c:
	print(''.join(i))


from itertools import combinations
a = input().split(' ')
for z in range(1, int(a[1]) + 1):
	for y in combinations(sorted(a[0]), z):
		for x in y:
			print(x, end='')
		print()


from itertools import combinations
(string, n) = input().split()
n = int(n)
o = []
if n <= len(string):
	for i in range(1, n + 1):
		l = sorted(list(combinations(string, i)))
		for x in sorted(l):
			st = ''
			for y in sorted(x):
				st += y
			o.append(st)
	for j in range(1, n + 1):
		for i in sorted(o):
			if len(i) == j:
				print(i)


from itertools import combinations
(S, k) = input().split()
for i in range(1, int(k) + 1):
	for j in combinations(sorted(S), i):
		print(''.join(j))


from itertools import combinations
(s, l) = input().split()
sorted_s = sorted(list(s))
for i in range(int(l)):
	for x in list(combinations(sorted_s, i + 1)):
		print(''.join(x))


from itertools import combinations
(s, k) = input().split()
for i in range(int(k)):
	for e in combinations(sorted(s), i + 1):
		print(''.join(e))


from itertools import *
word = input().split()
text = ''.join(sorted(word[0]))
n = int(word[1])
text_len = len(text)
premu = []
for i in range(1, n + 1):
	premu.extend(list(combinations(text, i)))
for i in premu:
	print(''.join(i))


from itertools import *
(s, k) = input().split()
l = []
for i in range(1, int(k) + 1):
	l = list(combinations(sorted(list(s)), i))
	for i in range(len(l)):
		l[i] = ''.join(l[i])
	l.sort()
	for i in l:
		print(i)


from itertools import combinations
(s, k) = input().split()
s = sorted(s)
k = int(k)
for i in range(1, k + 1):
	for c in combinations(s, i):
		print(''.join(c))


from itertools import combinations
(s, k) = input().split()
k = int(k)
for k in range(1, k + 1):
	for i in sorted(map(sorted, combinations(s, k))):
		print(''.join(i))


from itertools import combinations
A = list(input().split())
C = list()
Final = list()
d = ''
flag = True
for i in range(1, int(A[1]) + 1):
	C = list(combinations(A[0], i))
	C.sort()
	if flag is True:
		for j in range(len(C)):
			for k in C[j]:
				d += k
		A[0] = d
		d = ''
		flag = False
	for j in range(len(C)):
		for k in C[j]:
			d += k
		Final.append(d)
		d = ''
for i in Final:
	print(i)


from itertools import combinations
s = input()
(word, n) = s.split(' ')
n = int(n)
word = ''.join(sorted(word))
for i in range(1, n + 1):
	combos = list(combinations(word, i))
	for c in combos:
		print(''.join(c))


from itertools import combinations
(a, b) = input().split()
a = sorted(a)
b = int(b)
for i in range(1, b + 1):
	c = list(combinations(a, i))
	for d in c:
		print(''.join(d))


from itertools import combinations
I = input().split()
s = sorted(I[0])
c = [x for i in range(1, 1 + int(I[1])) for x in combinations(s, i)]
O = [''.join(x) for x in c]
print('\n'.join(O))


from itertools import combinations
tokens = input().split()
for i in range(1, int(tokens[1]) + 1):
	c = combinations(sorted(list(tokens[0])), i)
	print('\n'.join([''.join(x) for x in c]))


from itertools import combinations
(S, k) = input().split()
for i in range(1, int(k) + 1):
	comb = list(combinations(sorted(S), int(i)))
	for c in comb:
		print(''.join(c))


from itertools import combinations
(S, k) = input().split()
for r in range(int(k)):
	combs = combinations(sorted(list(S), key=lambda x: x[0]), r + 1)
	for x in list(combs):
		print(''.join(x))


from itertools import combinations
inp = input().split()
S = list(inp[0])
S.sort()
k = int(inp[1])
for i in range(1, k + 1):
	combinationArray = list(combinations(S, i))
	for combination in combinationArray:
		print(''.join(combination))


from itertools import combinations
[word, r] = list(input().split())
for i in range(1, int(r) + 1):
	for p in sorted(map(lambda p: sorted(p), combinations(word, i))):
		print(''.join(p))


from itertools import combinations
(s, n) = input().split(' ')
l = []
u = list(s)
u.sort()
o = ''
for i in u:
	o = o + i
for i in range(1, int(n) + 1):
	q = []
	k = list(combinations(o, i))
	for i in k:
		p = ''
		for j in i:
			p = p + j
		q.append(p)
	q.sort()
	for z in q:
		print(z)


from itertools import combinations
(mystr, k) = input().split()
mystr = sorted(mystr)
myperm = list()
for i in range(1, int(k) + 1):
	myperm.extend(list(combinations(mystr, int(i))))
myres = list()
for i in myperm:
	s = str()
	for x in i:
		s += x
	myres.append(s)
for i in myres:
	print(i)


from itertools import *
(s, k) = input().split()
s = sorted(s)
a = []
for i in range(1, int(k) + 1):
	for j in combinations(s, i):
		a.append(j)
for i in range(len(a)):
	str1 = ''.join(a[i])
	print(str1)


from itertools import combinations
(s, k) = input().split()
s = sorted(s)
for i in range(int(k)):
	for c in combinations(s, i + 1):
		print(''.join(c))


import itertools
line = input().split()
s = line[0]
k = int(line[1])
l = []
for i in range(1, k + 1):
	tmp = []
	for tup in itertools.combinations(s, i):
		tmp.append(''.join(sorted(tup)))
	for t in sorted(tmp):
		l.append(t)
for t in l:
	print(t)


from itertools import combinations
(s, k) = input().split()
s = sorted(s)
l = []
for i in range(1, int(k) + 1):
	for item in combinations(s, i):
		print(''.join(item))


from itertools import combinations
y = input().split()
s = list(y[0])
s.sort()
k = int(y[1])
for i in range(1, k + 1):
	l = list(combinations(s, i))
	for j in range(0, len(l)):
		print(''.join(list(l[j])))


from itertools import combinations
(s, i) = input().split()
i = int(i)
s = list(s)
s.sort()
for j in range(1, i + 1):
	for com in list(combinations(s, j)):
		print(''.join(com))


from itertools import combinations
(S, k) = str(input()).split()
ans = []
for i in range(1, int(k) + 1):
	S = list(S)
	S.sort()
	temp_list = list(combinations(S, i))
	[ans.append(element) for element in temp_list]
for s in ans:
	print(''.join(s))


from itertools import combinations
Sk = input().split()
S = sorted(Sk[0])
k = int(Sk[1])
for size in range(1, k + 1):
	[print(''.join(comb)) for comb in combinations(S, size)]


from itertools import combinations
(s, k) = input().split()
s = sorted(list(s))
result = []
for i in range(1, int(k) + 1):
	result += list(combinations(s, i))
for element in result:
	print(''.join(element))


from itertools import combinations
L = list(input().split())
S = L[0]
K = int(L[1])
NewList = []
List = []
for i in range(1, K + 1):
	L = list(combinations(S, i))
	NewList = NewList + L
for i in NewList:
	List.append(''.join(sorted(i)))
List = sorted(List)
for i in sorted(List, key=len):
	print(i)


from itertools import combinations
(word, num) = input().split()
(word, num) = (list(word), int(num))
for i in range(1, num + 1):
	combos = sorted([sorted(combo) for combo in combinations(word, i)])
	for combo in combos:
		print(''.join(combo))


from itertools import combinations
line = input().split()
string = ''.join(sorted(line[0]))
k = int(line[1])
outs = []
for length in range(1, k + 1):
	outs += list(combinations(string, length))
for item in outs:
	print('%s' % ''.join(item))


def combination():
	from itertools import combinations
	[s, N] = [x for x in input('').split(' ')]
	s = ''.join(sorted(s))
	i = 1
	while i <= int(N):
		length = list(combinations(s, i))
		for j in range(len(length)):
			print(str(length[j]).replace("('", '').replace("', '", '').replace("')", '').replace("',)", ''))
		i += 1
combination()


from itertools import combinations
(s, r) = input().split()
for i in range(1, int(r) + 1):
	for k in combinations(sorted(s), i):
		print(''.join(k))


from itertools import combinations
(s, k) = input().split()
s = sorted(s)
for i in range(1, int(k) + 1):
	for j in combinations(s, i):
		print(''.join(j))


l = input().split()
s = sorted(l[0])
k = int(l[1])
import itertools
for i in range(1, k + 1):
	for j in itertools.combinations(s, i):
		print(''.join(j))


from itertools import combinations
(s, k) = input().split()
for i in range(1, int(k) + 1):
	for e in combinations(sorted(s), i):
		print(''.join(e))


from itertools import combinations
args = input().split()
S = sorted(args[0])
k = int(args[1])
for i in range(1, k + 1):
	for c in list(combinations(S, i)):
		print(''.join(c))


from itertools import combinations
(s, k) = input().split()
(s, k) = (sorted(s), int(k))
for i in range(1, k + 1):
	for c in combinations(s, i):
		print(''.join(c))


from itertools import combinations
(word, size) = input().split()
for i in range(1, int(size) + 1):
	for j in list(combinations(sorted(word), i)):
		print(''.join((i for i in j)))


import itertools
(sr, kr) = input().split(' ')
s = sorted(list(sr))
k = int(kr)
for n in range(k):
	for i in itertools.combinations(s, n + 1):
		print(''.join(i))


from itertools import combinations
a = input()
b = list(a.split(' '))
s = sorted(b[0])
n = int(b[1])
r = [None] * n
for i in range(n):
	r[i] = list(combinations(s, i + 1))
for j in range(n):
	for i in r[j]:
		print(''.join(i))


import itertools

def read_int():
	return int(input())

def read_int_words():
	return map(int, input().split())
(S, ks) = input().split()
for ki in range(1, 1 + int(ks)):
	out = sorted(map(sorted, itertools.combinations(S, ki)))
	for c in out:
		print(''.join(map(str, c)))


from itertools import combinations
(n, s) = input().split()
m = int(s)
for i in range(m):
	for j in list(combinations(sorted(n), i + 1)):
		print(''.join(j))


from itertools import combinations
(wss, nb) = input().split()
nb = int(nb) + 1
for i in range(1, nb):
	ls = [''.join(sorted(ws)) for ws in combinations(wss, i) if ws]
	ls.sort()
	if ls:
		print(*ls, sep='\n')


from itertools import combinations
(A, B) = input().split()
for i in range(int(B)):
	for item in combinations(sorted(A), i + 1):
		print(''.join(item))


from itertools import *
(s, n) = input().split()
n = int(n) + 1
s = sorted(s)
for i in range(1, n):
	for j in combinations(s, i):
		print(''.join(j))


from itertools import *
a = input().split()
b = a[0]
c = int(a[1])
for i in range(1, c + 1):
	for g in combinations(sorted(b), i):
		print(''.join(g))


from itertools import combinations
(a, b) = input().split()
for i in range(1, int(b) + 1):
	for e in combinations(sorted(a), i):
		print(''.join(e))


from itertools import combinations
(a, n) = input().split()
combs = []
for i in range(int(n)):
	combs += sorted([''.join(sorted(x)) for x in combinations(a, i + 1)])
print('\n'.join(combs))


import itertools
input_str = input().split()
str = input_str[0]
str = list(str)
str.sort()
nums = int(input_str[1])
l = []
for num in range(1, nums + 1):
	ls = []
	for a in itertools.combinations(str, num):
		ls.append(''.join(a))
	ls.sort()
	l += ls
print('\n'.join(l))


from itertools import combinations
l = []
s = input().split()
n = int(s[1])
for i in range(1, n + 1):
	temp = []
	t = list(combinations(s[0], i))
	for j in t:
		tt = ''
		for k in j:
			tt += k
		temp.append(''.join(sorted(tt)))
	temp = sorted(temp)
	l.append(temp)
for i in l:
	for j in i:
		print(j)


from itertools import combinations
(txt, kk) = input().split(' ')
txt = list(txt)
txt.sort()
kk = int(kk)
for k in range(1, kk + 1):
	cc = list(map(''.join, combinations(txt, k)))
	cc.sort()
	for c in cc:
		print(c)


from itertools import combinations
(s, n) = (x for x in input().split())
n = int(n)
for i in range(1, n + 1):
	for x in combinations(sorted(list(s)), i):
		print(''.join(x))


from itertools import combinations

def __starting_point():
	in_data = list(input().strip().split(' '))
	for lng in range(1, int(in_data[1]) + 1):
		for el in combinations(sorted(in_data[0]), lng):
			print(''.join(el))
__starting_point()


from itertools import combinations
(S, k) = input().split()
k = int(k)
S = sorted(S)
for i in range(1, k + 1):
	for c in combinations(S, i):
		print(''.join(c))


import itertools
l = input().split()
for i in range(1, int(l[1]) + 1):
	l2 = itertools.combinations(l[0], i)
	l3 = []
	for n in l2:
		l3.append(sorted(n))
	for j in sorted(l3):
		for k in j:
			print(k, end='')
		print()


from itertools import combinations
(S, k) = input().split()
for i in range(1, int(k) + 1):
	print('\n'.join(sorted((''.join(sorted(j)) for j in combinations(S, i)))))


from itertools import combinations
tmp = input().split()
s = sorted(tmp[0])
n = int(tmp[1])
for i in range(1, n + 1):
	l = list(combinations(s, i))
	for c in l:
		print(''.join(c))


from itertools import combinations
(s, max_k) = input().split()
max_k = int(max_k)
for k in range(1, max_k + 1):
	for c in sorted((''.join(sorted(c)) for c in combinations(s, k))):
		print(c)