This is thematically similar to Generate keyboard shortcuts for a menu, but its actually a pretty different challenge.
Let's take PHPStorm's menu as an example:
New Project
New
Open Directory
Open
Open URL
New Project from Existing Files
Save As
Open Recent
Close Project
Rename Project
We need to assign a single-letter access keys for each of these menu items. Typically we pick the first letter, but if you notice, many of the above menu items start with the same letter!
Therefore, we'd want to pick:
P -> New Project
N -> New
D -> Open Directory
O -> Open
U -> Open URL
F -> New Project from Existing Files
S -> Save As
E -> Open Recent (We don't pick "R" here because "Rename Project" needs it)
C -> Close Project
R -> Rename Project
Therefore, for our list we need to maximize (in order of importance):
- The number of access keys actually contained in the string.
["X" -> "New"]
should be avoided unless its your only option.
- The number of access keys that start a word.
["N"->"New", "P"->"New Project"]
is better than["E"->"New", "N"->"New Project"]
- The number of access keys that start the sentence.
["N"->"New Project"]
is better than["P"->"New Project"]
Access keys are case-insensitive, so if your menu is ["Add", "add"]
, one of them will need to use D
as its access key.
Input/output
You need to input a list of strings. The strings will only contain alphabetic characters and spaces. You can optionally input a single string with a delimiter for each of the lines. You can assume there will be between 1 and 26 menu items (inclusive).
You need to output either:
- A Key-value map, where the key is the access key letter, and the value is the string it maps to
- A list of access key letters, where the Nth entry pairs with the Nth item in the list.
You can either return the objects themselves, or a reasonable string representation of them. Access key letters can be in any case.
Test Cases:
A -> A
Add,add -> A,D
Add To,Add -> T,A
Add To,Add Around,Add For -> T,A,F
Add To,Flip To,Add For -> T,F,A
Add,add,Dip,Around -> A,D,I,R
Add,add,Dad -> A,B,D
Add,add,Dad,dad,ad dadad,B -> A,C,D,E,Z,B
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,A,B,C -> A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,Add,Bz,Cx -> A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,Y,Z,X
D,A
would also be a valid output, right? What if priority number 1 can't be avoided? Test caseAdd,add,ADD
. \$\endgroup\$D,A
is valid output. I've got a test case for that already:Add,add,Dad -> A,B,D
\$\endgroup\$CamelCase
will not appear? Or doesn´t it matter? \$\endgroup\$