THINK FIRST·CODE LATER

← All labs

Permutations, including repeated letters

Problem

Write Permutations. Read a word and a limit. Generate all distinct permutations of its letters in alphabetical order with a recursive method that fills one position at a time using a used[] array; print the first limit of them, ... (k more) if there are more, and the total.

Repeated letters must not produce repeated results (aab has 3 permutations, not 6): sort the letters first, and skip a letter equal to its left neighbour when that neighbour is not currently used.

For the input

abc 10

the program prints

abc
acb
bac
bca
cab
cba
Distinct permutations: 6

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d1-4-data-1.txt

abc 10
What your program must print for d1-4-data-1.txt
abc
acb
bac
bca
cab
cba
Distinct permutations: 6

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d1-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content d1-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.