Pure StorageOA #2
Amazon Regex Generator
Estimated MediumJudge-ready
You are given uppercase strings x, y, z, each of length n. A regex is built from single uppercase letters or bracketed character classes, with no repeated letters inside a class, and the whole regex is concatenated position by position. Find the longest regex that matches both x and y but does not match z; if several answers have the same length, output the lexicographically smallest one; if none exists, output -1.
Input
The first line contains n (1≤n≤1000000); the next three lines are x, y, z in order, each containing exactly n uppercase English letters A-Z.
Output
Output the longest, lexicographically smallest regex that matches x/y but does not match z; if there is no solution, output -1.
Example 1
Input:
3
AAA
BBB
CCC
Output:
[ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABDEFGHIJKLMNOPQRSTUVWXYZ]
Example 2
Input:
4
ABCA
BCAB
CAAC
Output:
[ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABDEFGHIJKLMNOPQRSTUVWXYZ]
Example 3
Input:
3
AZA
ZAZ
AAA
Output:
-1
Editorial
Loading solution…