Python combinations_with_replacement of itertools

Python combinations_with_replacement of itertools

Instructor-svgAl-Mamun Sarkar
Mar 26 , 2020

Calculating combinations_with_replacement in Python. The following code shows how to calculate combinations_with_replacement in the python programming language. The following shows how to solve the HackerRank itertools.combinations_with_replacement() Python itertools problem.

Code:

import itertools

S, k = input().split() 
combinations = list(itertools.combinations_with_replacement(sorted(S), int(k)))
comb_list = [ ''.join(combination) for combination in combinations ]

for comb in comb_list:
	print(comb)

 

Test Input:

MNOP 2

Test Output:

MM
MN
MO
MP
NN
NO
NP
OO
OP
PP
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram