itertools.combinations_with_replacement(iterable, r) 

This tool returns $\textbf{r}$ length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.

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_with_replacement
>>> 
>>> print list(combinations_with_replacement('12345',2))
[('1', '1'), ('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '2'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '3'), ('3', '4'), ('3', '5'), ('4', '4'), ('4', '5'), ('5', '5')]
>>> 
>>> A = [1,1,3,3,3]
>>> print list(combinations(A,2))
[(1, 1), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (3, 3), (3, 3), (3, 3)]

Task

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

Your task is to print all possible size $\boldsymbol{\mbox{k}}$ replacement combinations 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 combinations with their replacements of string $\mbox{S}$ on separate lines.

Sample Input
HACK 2

Sample Output
AA
AC
AH
AK
CC
CH
CK
HH
HK
KK

Source: hackerrank | Difficulty: EASY


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


from itertools import combinations_with_replacement
(S, k) = input().split()
for i in list(combinations_with_replacement(sorted(S), int(k))):
	print(''.join(i))


from itertools import combinations_with_replacement
(a, b) = input().split()
a = sorted(a)
b = int(b)
for i in combinations_with_replacement(a, b):
	print(''.join(i))


from itertools import combinations_with_replacement
(s, k) = input().split()
s = sorted(s)
result = list(combinations_with_replacement(s, int(k)))
for element in result:
	print(''.join(element))


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


from itertools import combinations_with_replacement
(s, n) = input().split()
n = int(n)
for raw in combinations_with_replacement(sorted(list(s)), n):
	print(''.join((w for w in raw)))


from itertools import combinations_with_replacement as cr
(s, r) = input().split()
for k in cr(sorted(s), int(r)):
	print(''.join(k))


from itertools import combinations_with_replacement
(A, B) = input().split()
for item in list(combinations_with_replacement(sorted(A), int(B))):
	print(''.join(item))


from itertools import combinations_with_replacement
(s, m) = input().split()
l = list(s)
l.sort()
k = ''
for i in l:
	k = k + i
d = list(combinations_with_replacement(k, int(m)))
for i in d:
	q = ''
	for p in i:
		q = q + p
	print(q)


from itertools import combinations_with_replacement
(string, size) = input().split()
result = []
temp = list(combinations_with_replacement(sorted(string), int(size)))
for item in temp:
	result.append(''.join(item))
for item in result:
	print(item)


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


from itertools import combinations_with_replacement
(S, N) = input().split()
S = sorted(S)
N = int(N)
for i in combinations_with_replacement(S, N):
	print(''.join(sorted(i)))


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


from itertools import combinations_with_replacement
(S, k) = input().split()
k = int(k)
S = sorted(S)
for c in combinations_with_replacement(S, k):
	print(''.join(c))


import itertools
(s, k) = input().split()
for item in itertools.combinations_with_replacement(sorted(list(s)), int(k)):
	print(''.join(item))


from itertools import combinations_with_replacement
(S, k) = input().strip().split()
for i in list(combinations_with_replacement(sorted(S), int(k))):
	print(''.join(i))


from itertools import combinations_with_replacement
(s, k) = input().split()
s = sorted(s)
k = int(k)
p = list(combinations_with_replacement(s, k))
print(*[''.join(p[i]) for i in range(len(p))], sep='\n')


import itertools

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

def read_int_words():
	return map(int, input().split())
(S, ks) = input().split()
out = sorted(map(sorted, itertools.combinations_with_replacement(S, int(ks))))
for cr in out:
	print(''.join(map(str, cr)))


__author__ = 'sakshamchitkara'
from itertools import combinations_with_replacement
(str, limit) = input().split(' ')
for ele in list(combinations_with_replacement(sorted(str), int(limit))):
	print(''.join(ele))


from itertools import combinations_with_replacement
inp = input().split()
s = sorted(inp[0])
n = int(inp[1])
for l in list(combinations_with_replacement(s, n)):
	print(''.join(l))


from itertools import combinations_with_replacement
imput = input().split()
splitString = [c for c in imput[0]]
splitString.sort()
len = int(imput[1])
sol = combinations_with_replacement(splitString, len)
for a in sol:
	for e in a:
		print(e, end='')
	print()


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


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


from itertools import combinations_with_replacement
(S, k) = input().split()
k = int(k)
c = combinations_with_replacement(sorted(list(S)), k)
[print(''.join(i)) for i in c]


from itertools import combinations_with_replacement
(s, k) = input().split()
s = sorted(s)
k = int(k)
for i in combinations_with_replacement(s, k):
	print(''.join(i))


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


from itertools import combinations_with_replacement
(s, k) = input().split(' ')
k = int(k)
s = [x for x in s]
s.sort()
l = list(combinations_with_replacement(s, k))
l.sort()
for i in l:
	s = ''
	for k in i:
		s += k
	print(s)


from itertools import combinations_with_replacement
(s, k) = input().split()
s = sorted(s)
a = [''.join(p) for p in combinations_with_replacement(s, int(k))]
for x in a:
	print(x)


from itertools import combinations_with_replacement
(a, b) = input().split()
for e in combinations_with_replacement(sorted(a), int(b)):
	print(''.join(e))


from itertools import combinations_with_replacement
(a, n) = input().split()
print('\n'.join(sorted([''.join(sorted(x)) for x in combinations_with_replacement(a, int(n))])))


from itertools import combinations_with_replacement
(s, k) = input().split()
s = sorted(s)
for item in combinations_with_replacement(s, int(k)):
	print(''.join(item))


from itertools import combinations_with_replacement
inp = input().split()
S = list(inp[0])
S.sort()
k = int(inp[1])
arr = list(combinations_with_replacement(S, k))
for combination in arr:
	print(''.join(combination))


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


from itertools import combinations_with_replacement
(S, k) = input().split()
for a in combinations_with_replacement(sorted(S), int(k)):
	print(''.join(a))


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


from itertools import combinations_with_replacement
(s, i) = input().split()
i = int(i)
s = list(s)
s.sort()
for com in list(combinations_with_replacement(s, i)):
	print(''.join(com))


import itertools
(S, k) = input().split()
print('\n'.join((''.join(x) for x in itertools.combinations_with_replacement(sorted(S), int(k)))))


from itertools import combinations_with_replacement
(S, k) = input().split()
k = int(k)
for i in combinations_with_replacement(sorted(S), k):
	print(''.join(i))


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


from itertools import combinations_with_replacement
(txt, l) = input().split()
l = int(l)
txt = list(txt)
txt.sort()
q = list(combinations_with_replacement(txt, l))
q.sort()
for i in q:
	print(''.join(i))


from itertools import combinations_with_replacement
(s, l) = input().split()
sorted_s = sorted(list(s))
for x in list(combinations_with_replacement(sorted_s, int(l))):
	print(''.join(x))


from itertools import *
(s, n) = input().split()
n = int(n)
s = sorted(s)
for j in combinations_with_replacement(s, n):
	print(''.join(j))


from itertools import combinations_with_replacement
(S, k) = input().split()
k = int(k)
print('\n'.join([''.join(x) for x in combinations_with_replacement(list(sorted(S, key=lambda x: x[0])), k)]))


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


(s, k) = input().split()
s = sorted(s)
k = int(k)
import itertools
for i in itertools.combinations_with_replacement(s, k):
	print(''.join(i))


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


from itertools import combinations_with_replacement
(S, k) = input().split()
S = sorted(S)
k = int(k)
res = list(combinations_with_replacement(S, k))
for i in res:
	for ch in i:
		print(ch, end='')
	print()


from itertools import *
(s, k) = input().split()
s = ''.join(sorted(s))
k = int(k)
for c in combinations_with_replacement(s, k):
	print(''.join(c))


import itertools
Line = str(input()).split()
S = Line[0]
k = int(Line[1])
L = sorted(list(itertools.combinations_with_replacement(S, k)))
Temp = []
for T in L:
	String = ''.join(sorted(T))
	Temp.append(String)
Temp = sorted(Temp)
for i in range(0, len(Temp)):
	print(Temp[i])


from itertools import combinations_with_replacement
(S, l) = input().split(' ')
for c in list(combinations_with_replacement(sorted(S), int(l))):
	print(''.join(c))


from itertools import combinations_with_replacement
Sk = input().split()
[print(''.join(combWithRep)) for combWithRep in combinations_with_replacement(sorted(Sk[0]), int(Sk[1]))]


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


from itertools import combinations_with_replacement
(S, k) = input().split()
print('\n'.join(sorted((''.join(sorted(j)) for j in combinations_with_replacement(S, int(k))))))


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


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


from itertools import combinations_with_replacement
(word, num) = input().split()
p = combinations_with_replacement(sorted(word), int(num))
for i in p:
	print(''.join(i))


from itertools import combinations_with_replacement
(S, k) = input().split()
k = int(k)
S = sorted(S)
[print(''.join(i)) for i in list(combinations_with_replacement(S, k))]


from itertools import combinations_with_replacement
(s, n) = (x for x in input().split())
n = int(n)
for x in combinations_with_replacement(sorted(list(s)), n):
	print(''.join(x))


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


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


from itertools import combinations_with_replacement
(S, k) = input().split()
for i in combinations_with_replacement(sorted(S), int(k)):
	print(''.join(i))


from itertools import combinations_with_replacement
(mystr, k) = input().split()
mystr = sorted(mystr)
myperm = list(list(combinations_with_replacement(mystr, int(k))))
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 combinations_with_replacement
(word, num) = input().split()
(word, num) = (sorted(list(word)), int(num))
for combo in combinations_with_replacement(word, num):
	print(''.join(combo))


from itertools import combinations_with_replacement
[word, r] = list(input().split())
word = sorted(word)
for p in combinations_with_replacement(word, int(r)):
	print(''.join(p))


(s, k) = input().split()
from itertools import combinations_with_replacement
s = ''.join(sorted(s))
print('\n'.join((''.join(p) for p in combinations_with_replacement(s, int(k)))))


from itertools import combinations_with_replacement
(s, k) = input().split()
l = list(combinations_with_replacement(s, int(k)))
l.sort()
a = list()
for g in l:
	c = list(g)
	c.sort()
	a.append(''.join(c))
a.sort()
for each in a:
	print(each)


def permutation():
	from itertools import combinations_with_replacement
	[s, N] = [x for x in input('').split(' ')]
	s = ''.join(sorted(s))
	length = list(combinations_with_replacement(s, int(N)))
	list1 = combinations_with_replacement(s, int(N))
	for i in range(len(length)):
		print(str(length[i]).replace("('", '').replace("', '", '').replace("')", '').replace("',)", ''))
permutation()


from itertools import combinations_with_replacement
(s, k) = input().split()
k = int(k)
for c in sorted((''.join(sorted(c)) for c in combinations_with_replacement(s, k))):
	print(c)


from itertools import combinations_with_replacement
(s, n) = input().split()
s = sorted(list(s))
n = int(n)
c = combinations_with_replacement(s, n)
for i in c:
	print(''.join(i))


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


from itertools import combinations_with_replacement
(S, k) = input().split()
comb = list(combinations_with_replacement(sorted(S), int(k)))
for c in comb:
	print(''.join(c))


from itertools import combinations_with_replacement
(S, k) = str(input()).split()
S = list(S)
S.sort()
ans = list(combinations_with_replacement(S, int(k)))
for s in ans:
	print(''.join(s))


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


from itertools import combinations_with_replacement
(S, k) = input().split(' ')
S = sorted(S)
k = int(k)
for i in list(combinations_with_replacement(S, k)):
	print(''.join(i))


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


from itertools import combinations_with_replacement
(string, n) = input().split()
n = int(n)
string = sorted(string)
l = list(combinations_with_replacement(string, n))
for i in l:
	for j in i:
		print(j, end='')
	print()


from itertools import combinations_with_replacement
(s, n) = input().split()
n = int(n)
print('\n'.join(map(''.join, combinations_with_replacement(sorted(s), n))))


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


from itertools import combinations_with_replacement
(sr, kr) = input().split(' ')
s = sorted(list(sr))
k = int(kr)
for i in combinations_with_replacement(s, k):
	print(''.join(i))


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


import itertools
(s, k) = input().split()
for i in itertools.combinations_with_replacement(sorted(s), int(k)):
	print(''.join(i))


from itertools import combinations_with_replacement
tokens = input().split()
p = combinations_with_replacement(sorted(list(tokens[0])), int(tokens[1]))
print('\n'.join([''.join(x) for x in p]))


from itertools import combinations_with_replacement
s = input()
(word, n) = s.split(' ')
n = int(n)
word = ''.join(sorted(word))
combos = combinations_with_replacement(word, n)
for c in combos:
	print(''.join(c))


from itertools import combinations_with_replacement
(s, n) = input().split()
s = list(s)
s.sort()
''.join(s)
l = list(combinations_with_replacement(s, int(n)))
l.sort()
for i in l:
	print(''.join(i))


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


from itertools import combinations_with_replacement
(N, k) = input().split()
k = int(k)
l = [''.join(sorted(x)) for x in combinations_with_replacement(N, k)]
print('\n'.join(sorted(l)))


from itertools import combinations_with_replacement
(m, n) = input().split()
k = int(n)
for i in list(combinations_with_replacement(sorted(m), k)):
	print(''.join(i))


from itertools import combinations_with_replacement

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


from itertools import combinations_with_replacement
x = list(input().split())
y = int(x[1])
for i in sorted(list(combinations_with_replacement(sorted(x[0]), y))):
	print(''.join(i))


from itertools import combinations_with_replacement
(s, k) = input().split()
for c in combinations_with_replacement(sorted(s), int(k)):
	print(''.join(c))


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


from itertools import combinations_with_replacement
(s, k) = input().split()
print(*map(lambda x: ''.join(x), combinations_with_replacement(''.join(sorted(s)), int(k))), sep='\n')


import itertools
l = input().split()
l1 = itertools.combinations_with_replacement(l[0], int(l[1]))
l2 = []
for k in l1:
	l2.append(sorted(k))
for i in sorted(l2):
	for j in i:
		print(j, end='')
	print()


from itertools import combinations_with_replacement
(s, k) = input().split()
for e in combinations_with_replacement(sorted(s), int(k)):
	print(''.join(e))


import itertools
(a, b) = input().split()
for e in itertools.combinations_with_replacement(sorted(a), int(b)):
	print(''.join(e))


from itertools import combinations_with_replacement
(word, num) = input().split()
k = combinations_with_replacement(sorted(word), int(num))
for i in k:
	print(''.join(i))


from itertools import combinations_with_replacement
A = list(input().split())
C = list()
d = ''
temp = list(A[0])
temp.sort()
for i in temp:
	d += i
A[0] = d
d = ''
B = list(combinations_with_replacement(A[0], int(A[1])))
for i in range(len(B)):
	for j in B[i]:
		d += j
	C.append(d)
	d = ''
C.sort()
for i in C:
	print(i)