Menu Shortcuts
Traditionally, user menus are accessible by keyboard shortcuts, such as Alt + (a letter)
, or even simply hitting the letter when all textboxes are unfocused (gmail style).
Your task
Given the menu entries as an input, your task is to grant each menu entry a proper shortcut letter.
Write a function or a program that accepts a set of words - the menu entries (as an array of strings, or your language equivalent), and returns a dictionary, or a hashmap, from a single letter to a menu entry.
You can either use a parameter and return a value, or use the STDIN and output your results to STDOUT. You are not allowed to assume a global/scope variable is already populated with the input.
Algorithm to determine the proper letter
- Basically it's the first available letter of the word. See assumptions and examples below.
- In case all entry's letters are not available, the shortcut will be
(a letter) + (a number)
. Which letter you pick from the entry is arbitrary. The number should start from 0 and be incremented by 1 - such that all shortcuts are unique. See third example below.
Assumptions
- The input will be a Set, i.e. no repetitions, every entry is unique.
- Length of the input can be any non-negative integer (up to MAX_INT of your language).
- Case sensitivity: Input is case-sensitive, (but will stay unique when ignoring case). The results should contain the original entries with their original casing. However, the output shortcut letters are not case-sensitive.
- All input words will not end with numbers.
- No "evil input" will be tested. "Evil input" is such that you have to increment the counter of a certain letter more than 10 times.
Examples
Examples below are in JSON, but you can use your language equivalent for an array and a Dictionary, or - in case you're using STD I/O - any readable format for your input and output (such as csv, or even space-separated values).
1.
Input: ['File', 'Edit', 'View', 'Help']
Output: {f:'File', e:'Edit', v:'View', h:'Help'}
2.
Input: ['Foo', 'Bar', 'FooBar', 'FooBars']
Output: {f:'Foo', b:'Bar', o:'FooBar', a:'FooBars'}
3.
Input: ['a', 'b', 'aa', 'bb', 'bbq', 'bbb', 'ba']
Output: {a:'a', b:'b', a0:'aa', b0:'bb', q:'bbq', b1:'bbb', b2:'ba'}
Winning conditions
Shortest code wins. Only ASCII is allowed.
['ab', 'a']
give{a:'ab', a0:'a'}
or{b:'ab', a:'a'}
? \$\endgroup\$