Sitemap

Hackerrank problem 4

1 min readFeb 13, 2024

--

itertools.permutations()

This tool returns successive length permutations of elements in an iterable.

If is not specified or is None, then defaults to the length of the iterable, and all possible full length permutations are generated.

Permutations are printed in a lexicographic sorted order. So, if the input iterable is sorted, the permutation tuples will be produced in a sorted order.

Task

You are given a string .
Your task is to print all possible permutations of size of the string in lexicographic sorted order.

Input Format

Get Vipul Kaitke’s stories in your inbox

Join Medium for free to get updates from this writer.

A single line containing the space separated string and the integer value .

Constraints

The string contains only UPPERCASE characters.

Output Format

Print the permutations of the string on separate lines.

Explanation

All possible size permutations of the string “HACK” are printed in lexicographic sorted order.

# Enter your code here. Read input from STDIN. Print output to STDOUT

from itertools import permutations
str1, int1 = input().split()

for i in sorted(permutations(str1, int(int1))):
print (''.join(i))

#VEV CODE LAB

--

--