// 2024 IUSB Programming Competition // Round 2 Problem 2 // Word Scamble // Solution by Liguo Yu import java.util.Scanner; import java.util.*; public class round2_p2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String word = input.next(); Set words = Generate(word); List sortedList = new ArrayList<>(words); Collections.sort(sortedList); Iterator wordsIterator = sortedList.iterator(); while(wordsIterator.hasNext()) { System.out.println(wordsIterator.next()); } } public static Set Generate(String word) { Set perm = new HashSet<>(); if (word == null) return null; else if (word.length() == 0) { perm.add(""); return perm; } char initial = word.charAt(0); String rem = word.substring(1); Set words = Generate(rem); for (String strNew : words) { for (int i = 0;i<=strNew.length();i++) { perm.add(charInsert(strNew, initial, i)); } } return perm; } public static String charInsert(String str, char c, int j) { String begin = str.substring(0, j); String end = str.substring(j); return begin + c + end; } }