import java.util.*; public class Main { private static boolean match(String s, int[] perm, String target){ if(s.length() != target.length()) return false; for(int i=0; i, Iterable { private int[] next; public Permutation(int n) { next = java.util.stream.IntStream.range(0, n).toArray(); } @Override public boolean hasNext() { return next != null; } @Override public int[] next() { int[] r = next.clone(); next = nextPermutation(next); return r; } @Override public java.util.Iterator iterator() { return this; } public static int[] nextPermutation(int[] a) { if (a == null || a.length < 2) return null; int p = 0; for (int i = a.length - 2; i >= 0; i--) { if (a[i] >= a[i + 1]) continue; p = i; break; } int q = 0; for (int i = a.length - 1; i > p; i--) { if (a[i] <= a[p]) continue; q = i; break; } if (p == 0 && q == 0) return null; int temp = a[p]; a[p] = a[q]; a[q] = temp; int l = p, r = a.length; while (++l < --r) { temp = a[l]; a[l] = a[r]; a[r] = temp; } return a; } }