Loading...
Loading...
You are given a list of strings commands. Each command is a binary string made of the characters 0 and 1. The commands are typed one at a time, in order.
The common prefix of two strings is the longest string that both of them start with. For example, the common prefix of "0110" and "0101" is "01".
When a command is typed, it gets a suggestion: one of the commands typed before it. The suggestion is chosen like this:
An earlier command that is identical to the one being typed shares all of its characters.
If no earlier command shares even the first character, every earlier command ties with a common prefix of length 0, so the suggestion is the most recently typed command. The first command has no earlier commands, so it gets no suggestion.
Return a list result where result[i] is the 1-based position in commands of the suggestion for commands[i]. If commands[i] gets no suggestion, result[i] is 0.
Input: commands = ["000","1110","01","001","110","11"]
Output: [0,1,1,1,2,5]
Explanation: "000" is the first command, so it gets 0.
"1110" shares no prefix with "000", so the most recent command, 1, is suggested.
"01" shares "0" with "000" and nothing with "1110", so command 1 wins.
"001" shares "00" with "000" but only "0" with "01", so command 1 wins.
"110" shares "11" with "1110" and nothing with the others, so command 2 wins.
"11" shares "11" with both "1110" (command 2) and "110" (command 5). That is a tie, so the more recent command 5 wins.
Input: commands = ["1","1","0"]
Output: [0,1,2]
Explanation: The second "1" is identical to command 1, so it shares its whole length and command 1 is suggested.
"0" shares no prefix with either earlier command, so the most recent one, command 2, is suggested.
Input: commands = ["10","101","10"]
Output: [0,1,2]
Explanation: The last "10" is identical to command 1, but command 2 ("101") also starts with "10".
Both share 2 characters with it, so the more recent command 2 wins.
commands.length ≤105commands[i].lengthcommands is at most 2⋅105commands[i] consists only of the characters 0 and 1