import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.*;
import java.util.stream.Collectors;
class Ideone
{
{
StringBuilder b = new StringBuilder();
List<String> combinations = new ArrayList<>();
combine(a, b, 0, 0, combinations);
List<String> filtered = combinations.stream()
.filter(t -> t.length() == 2)
.collect(Collectors.toList());
filtered.stream()
.forEach(t -> {
List<String> permutations = new ArrayList<>();
StringBuilder sb = new StringBuilder();
boolean used[] = new boolean[2];
used[0] = false;
used[1] = false;
permute(t, sb, used, permutations);
}
});
}
public static void combine
(String a, StringBuilder b,
int start,
int level, List
<String
> combinations
) {
for (int i = start; i < a.length(); i++) {
if (b.
toString().
contains(String.
valueOf(a.
charAt(i
))) == false) {
b.append(a.charAt(i));
combinations.add(b.toString());
if (i < a.length() - 1) {
combine(a, b, start + 1, level + 1, combinations);
}
b.setLength(b.length() - 1);
}
}
}
public static void permute
(String a, StringBuilder b,
boolean[] used, List
<String
> permutations
) {
if ( b.length() == a.length()) {
permutations.add(b.toString());
return;
}
for (int i = 0; i < a.length(); i++) {
if (used[i]) {continue;}
used[i] = true;
b.append(a.charAt(i));
permute(a, b, used, permutations);
b.setLength(b.length()-1);
used[i] = false;
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnV0aWwuc3RyZWFtLio7CmltcG9ydCBqYXZhLnV0aWwuc3RyZWFtLkNvbGxlY3RvcnM7IAoKY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoJCVN0cmluZyBhID0gIkFCQyI7IAoKICAgICAgICBTdHJpbmdCdWlsZGVyIGIgPSBuZXcgU3RyaW5nQnVpbGRlcigpOyAKICAgICAgICAKICAgICAgICBMaXN0PFN0cmluZz4gY29tYmluYXRpb25zID0gbmV3IEFycmF5TGlzdDw+KCk7CiAgICAgICAgCiAgICAgICAgY29tYmluZShhLCBiLCAwLCAwLCBjb21iaW5hdGlvbnMpOwogICAgICAgIAogICAgICAgIExpc3Q8U3RyaW5nPiBmaWx0ZXJlZCA9IGNvbWJpbmF0aW9ucy5zdHJlYW0oKQogICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC5maWx0ZXIodCAtPiB0Lmxlbmd0aCgpID09IDIpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLmNvbGxlY3QoQ29sbGVjdG9ycy50b0xpc3QoKSk7CgogICAgICAgIAogICAgICAgIGZpbHRlcmVkLnN0cmVhbSgpCiAgICAgICAgCiAgICAgICAgICAgICAgICAuZm9yRWFjaCh0IC0+IHsKICAgICAgICAgICAgICAgIAkKICAgICAgICAgICAgICAgIAkgICAgICAgIExpc3Q8U3RyaW5nPiBwZXJtdXRhdGlvbnMgPSBuZXcgQXJyYXlMaXN0PD4oKTsKICAgICAgICAgICAgICAgIAkgICAgICAgIAogICAgICAgICAgICAgICAgCSAgICAgICAgU3RyaW5nQnVpbGRlciBzYiA9IG5ldyBTdHJpbmdCdWlsZGVyKCk7CiAgICAgICAgICAgICAgICAJICAgICAgICAKCQkJCQkgICAgICAgIGJvb2xlYW4gdXNlZFtdID0gbmV3IGJvb2xlYW5bMl07IAoJCQkJCSAgICAgICAgCgkJCQkJICAgICAgICB1c2VkWzBdID0gZmFsc2U7IAoJCQkJCSAgICAgICAgCgkJCQkJICAgICAgICB1c2VkWzFdID0gZmFsc2U7IAoJCQkJCSAgICAgICAgCgkJCQkJICAgICAgICBwZXJtdXRlKHQsIHNiLCB1c2VkLCBwZXJtdXRhdGlvbnMpOyAKCQkJCQkgICAgICAgIAoJCQkJCSAgICAgICAgZm9yKFN0cmluZyBzOiBwZXJtdXRhdGlvbnMpeyAKCQkJCQkgICAgICAgIAkKCQkJCQkgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocyk7IAoJCQkJCSAgICAgICAgICAgIAoJCQkJCSAgICAgICAgfSAKCQkJCQkgICAgICAgIAogICAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgICAgICAKCX0KCQoJcHVibGljIHN0YXRpYyB2b2lkIGNvbWJpbmUoU3RyaW5nIGEsIFN0cmluZ0J1aWxkZXIgYiwgaW50IHN0YXJ0LCBpbnQgbGV2ZWwsIExpc3Q8U3RyaW5nPiBjb21iaW5hdGlvbnMpIHsgCgogICAgIGZvciAoaW50IGkgPSBzdGFydDsgaSA8IGEubGVuZ3RoKCk7IGkrKykgeyAKICAgICAJCiAgICAgICAgaWYgKGIudG9TdHJpbmcoKS5jb250YWlucyhTdHJpbmcudmFsdWVPZihhLmNoYXJBdChpKSkpID09IGZhbHNlKSB7IAogICAgICAgIAkKICAgICAgICAgICAgYi5hcHBlbmQoYS5jaGFyQXQoaSkpOyAKICAgICAgICAgICAgCiAgICAgICAgICAgIGNvbWJpbmF0aW9ucy5hZGQoYi50b1N0cmluZygpKTsgCiAgICAgICAgICAgIAogICAgICAgICAgICBpZiAoaSA8IGEubGVuZ3RoKCkgLSAxKSB7IAogICAgICAgICAgICAJCiAgICAgICAgICAgICAgICBjb21iaW5lKGEsIGIsIHN0YXJ0ICsgMSwgbGV2ZWwgKyAxLCBjb21iaW5hdGlvbnMpOyAKICAgICAgICAgICAgICAgIAogICAgICAgICAgICB9IAogICAgICAgICAgICAKICAgICAgICAgICAgYi5zZXRMZW5ndGgoYi5sZW5ndGgoKSAtIDEpOyAKICAgICAgICAgICAgCiAgICAgICAgfSAKICAgICAgICAKICAgICB9IAoKICB9IAogIAogICAgcHVibGljIHN0YXRpYyB2b2lkIHBlcm11dGUoU3RyaW5nIGEsIFN0cmluZ0J1aWxkZXIgYiwgYm9vbGVhbltdIHVzZWQsIExpc3Q8U3RyaW5nPiBwZXJtdXRhdGlvbnMpIHsgCgogICAgICAgIGlmICggYi5sZW5ndGgoKSA9PSBhLmxlbmd0aCgpKSB7IAogICAgICAgIAkKICAgICAgICAgICAgcGVybXV0YXRpb25zLmFkZChiLnRvU3RyaW5nKCkpOyAKICAgICAgICAgICAgCiAgICAgICAgICAgIHJldHVybjsgCiAgICAgICAgfSAKCiAgICAgICAgZm9yIChpbnQgaSAgPSAgMDsgaSA8IGEubGVuZ3RoKCk7IGkrKykgeyAKCiAgICAgICAgICAgIGlmICh1c2VkW2ldKSB7Y29udGludWU7fSAKCiAgICAgICAgICAgIHVzZWRbaV0gPSB0cnVlOyAKCiAgICAgICAgICAgIGIuYXBwZW5kKGEuY2hhckF0KGkpKTsgCgogICAgICAgICAgICBwZXJtdXRlKGEsIGIsIHVzZWQsIHBlcm11dGF0aW9ucyk7IAoKICAgICAgICAgICAgYi5zZXRMZW5ndGgoYi5sZW5ndGgoKS0xKTsgCgogICAgICAgICAgICB1c2VkW2ldID0gZmFsc2U7IAoKICAgICAgICB9ICAKCiAgICB9IAoKfSAK