13
\$\begingroup\$

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):

  1. The number of access keys actually contained in the string.
    • ["X" -> "New"] should be avoided unless its your only option.
  2. The number of access keys that start a word.
    • ["N"->"New", "P"->"New Project"] is better than ["E"->"New", "N"->"New Project"]
  3. 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:

  1. A Key-value map, where the key is the access key letter, and the value is the string it maps to
  2. 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
\$\endgroup\$
11
  • \$\begingroup\$ What about non-unique outputs? I assume for your test case D,A would also be a valid output, right? What if priority number 1 can't be avoided? Test case Add,add,ADD. \$\endgroup\$ Sep 13, 2016 at 18:19
  • \$\begingroup\$ D,A is valid output. I've got a test case for that already: Add,add,Dad -> A,B,D \$\endgroup\$ Sep 13, 2016 at 18:21
  • \$\begingroup\$ Ah okay, might be worth mentioning those things in the spec. "...should be avoided at all costs" sounds like it's an invalid solution. \$\endgroup\$ Sep 13, 2016 at 18:22
  • \$\begingroup\$ Can we assume that only first letters in a word are upper case, i.e. that CamelCase will not appear? Or doesn´t it matter? \$\endgroup\$
    – Titus
    Sep 13, 2016 at 22:44
  • \$\begingroup\$ No. Words are defined by whitespace, not by case. \$\endgroup\$ Sep 13, 2016 at 22:45

1 Answer 1

3
+150
\$\begingroup\$

Jelly, 41 bytes

e/€S;eḲ1ịⱮƊ}¥/€;=Ḣ¥/€µS
LØAœ!ż€³Œu¤ÇÐṀẎḢ€

Try it online!

This is really messy and also O(26^n) lol.

Explanation: literally just brute forces it ._.

-6 bytes thanks to caird coinheringaahing

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 45 bytes \$\endgroup\$ Oct 1, 2020 at 15:10
  • \$\begingroup\$ @cairdcoinheringaahing ah, nice you figured it out :D thank you \$\endgroup\$
    – hyper-neutrino
    Oct 1, 2020 at 15:11
  • \$\begingroup\$ 41 bytes \$\endgroup\$ Oct 1, 2020 at 15:26
  • \$\begingroup\$ @cairdcoinheringaahing Oh, yeah, I didn't realize that since the conditions are subsets of each other I don't need to separate them and keep them in order lol. Thanks! \$\endgroup\$
    – hyper-neutrino
    Oct 1, 2020 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.