String Restoration
Practice
3.4 (13 votes)
Basic programming
Basics of implementation
Easy
Implementation
Problem
68% Success 8320 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Alice was given a string \(S\). She made a prefix array \(P\) out of it. The value of prefix array i.e. \(P[i]\) is the count of distinct characters in the substring of string \(S\) from position \(0\) to \(i\) . Since the string is lost, you need to print a string that satisfies this prefix array.

If there is more than one correct answer, print the lexicographically smallest of them. If there is no such string then you need to print \(-1\) .

Input
The first line contains an integer \(T\) that denotes the total number of test cases.
The first line of each test case contains an integer \(N\) that denotes the length of the prefix array \(P\). (Note that the length of string \(S\) is also \(N\)).
The second line of each test case contains \(N\) space separated integers that denote the value of array \(P\).

Output
In the output, you need to print \(-1\) if there is no possible string or the smallest possible string that satisfies the prefix array constraint.

Constraints
\(1 \le T \le 10\)

\(1 \le N \le 100000\)
\(1 \le P[i] \le N\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
30 votes
Tags:
Basic ProgrammingBasics of ImplementationCombinatoricsEasyImplementation
Points:30
31 votes
Tags:
AlgorithmsApprovedBit ManipulationDynamic ProgrammingMediumOpen
Points:20
7 votes
Tags:
Basic ProgrammingBasics of ImplementationEasyImplementation