Password Clues and the Next Command
Problem statement
Choose the next command for a password puzzle. The list files gives the filenames in order. The aligned array read contains 1 for a file that has already been read and 0 for an unread file.
If any file is unread, return READ filename for the first unread file in the given list. Return immediately: the current clues may be incomplete or contradictory while files remain unread.
When all files have been read, determine a password of length passwordLength. Each entry in clues has the form p:[letters]. The positive decimal integer p is a one-based password position, and letters is a nonempty string of lowercase English letters. For example, 2:[abc] means the second character is one of a, b, or c. Repeated letters in one clue have no extra effect.
Each position starts with all 26 lowercase English letters as possibilities. Intersect all clue sets for that position. In an all-read state, every position is guaranteed to have exactly one possible character after these intersections. Return ROOT password, with the characters arranged from position 1 through passwordLength.
Commands contain exactly one space after READ or ROOT. This function returns one command for the supplied state; it does not read files or change their flags.
Function
nextPasswordCommand(files: String[], read: int[], passwordLength: int, clues: String[]) → StringExamples
Example 1
files = ["notes","hint"]read = [1,0]passwordLength = 2clues = ["1:[ab]"]return = "READ hint"The first file is already read, and hint is the first unread file. Return READ hint without trying to complete the password from the partial clues.
Example 2
files = ["a","b"]read = [1,1]passwordLength = 3clues = ["1:[cat]","2:[dog]","3:[xy]","1:[ab]","2:[go]","2:[g]","3:[zy]"]return = "ROOT agy"All files have been read. At position 1, {c,a,t} intersected with {a,b} leaves a. At position 2, {d,o,g}, {g,o}, and {g} leave g. At position 3, {x,y} intersected with {z,y} leaves y. Return ROOT agy.
Example 3
files = []read = []passwordLength = 1clues = ["1:[z]"]return = "ROOT z"There are no unread files. The only clue fixes the single password character to z, so return ROOT z.
Constraints
- 0 <= files.length <= 100.
- read.length == files.length.
- Every read value is 0 or 1.
- Each filename has length between 1 and 30 and contains only lowercase English letters, digits, and underscores. Filenames are unique.
- 1 <= passwordLength <= 100.
- 0 <= clues.length <= 2600.
- Each clue has length between 5 and 32.
- Every clue exactly follows p:[letters], with no spaces or leading zeros in p, 1 <= p <= passwordLength, and between 1 and 26 lowercase English letters.
- If all files are read, each password position has exactly one possible character after all clue intersections. If any file is unread, this uniqueness condition is not required.