Programming contests

ILBPM9987L Informatikai versenyfeladatok, 2023. ősz

November 17, 2023 8:00 PM – January 31, 2024 11:59 PM


You are to write a program that generates all possible words from a given set of letters.

Example: Given the word “abc”, your program should – by exploring all different combinations of the three letters – output the words “abc”, “acb”, “bac”, “bca”, “cab”, and “cba”.

In the word taken from the input, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order.

Input Specification

The input consists of several words. The first line contains the number of words to follow. Each of the following lines contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different.

Output Specification

For each word in the input, the output should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output one per line, in alphabetically ascending order. An uppercase letter goes before the corresponding lowercase letter.

Sample Input

  1. 3
  2. aAb
  3. abc
  4. acba
download as text file

Output for Sample Input

  1. Aab
  2. Aba
  3. aAb
  4. abA
  5. bAa
  6. baA
  7. abc
  8. acb
  9. bac
  10. bca
  11. cab
  12. cba
  13. aabc
  14. aacb
  15. abac
  16. abca
  17. acab
  18. acba
  19. baac
  20. baca
  21. bcaa
  22. caab
  23. caba
  24. cbaa
download as text file
University of Debrecen; Faculty of Informatics; v. 03/01/2019