#K3101. Taco Lexicographical Permutations with Prefix
Taco Lexicographical Permutations with Prefix
Taco Lexicographical Permutations with Prefix
You are given a string s
(with 1 ≤ |s| ≤ 9) and a prefix string p
(with 0 ≤ |p| ≤ |s|). Your task is to generate all unique permutations of s
that start with the prefix p
, and output them in lexicographical order. If no such permutation exists, output -1
.
Note: If p
contains any character not present in s
(considering frequency as in the original string), then no valid permutation exists and the answer should be -1
. When p
is an empty string, all permutations of s
should be output.
inputFormat
The input is read from standard input and consists of two lines:
- The first line contains the string
s
. - The second line contains the prefix
p
(which may be empty).
outputFormat
Output to standard output all valid permutations each in a separate line, in lexicographical order. If no valid permutation exists, output a single line containing -1
.
abc
ab
abc
</p>