Given an array of letters in the range 'a' to 'o', compute how to construct the array by successively inserting the letters in alphabetical order. You will always start the insertion with a base array of all the 'o's that are in the array to be reconstructed.
Examples
Let the input array be:
['o', 'b', 'o', 'b', 'a']
The base would have been ['o', 'o'] in this case. To construct it you would do the following insertings.
- Insert 'a' at index 2 (indexing from 0). This gives ['o', 'o', 'a']
- Insert 'b at index 1. This gives ['o', 'b', 'o', 'a']
- Insert 'b' at index 3. This gives ['o', 'b', 'o', 'b', 'a']
Let the input array be:
['a', 'o', 'b', 'o', 'a', 'c', 'b']
The base case to start inserting will therefore be: ['o', 'o']
- Insert 'a' at index 0 giving ['a', 'o', 'o']
- Insert 'a' at index 3 giving ['a', 'o', 'o', 'a']
- Insert 'b' at index 2 giving ['a', 'o', 'b', 'o', 'a']
- Insert 'b' at index 5 giving ['a', 'o', 'b', 'o', 'a', 'b']
- Insert 'c' at index 5 giving ['a', 'o', 'b', 'o', 'a', 'c', 'b']
Let the input array be:
['c', 'b', 'a', 'o', 'o', 'o']
The base case to start inserting will therefore be: ['o', 'o', 'o']
- Insert 'a' at index 0
- Insert 'b' at index 0
- Insert 'c' at index 0
Let the input array be:
['c', 'b', 'a', 'o', 'b', 'o', 'b', 'a']
Output
The exact output format is of your choosing but it should be equivalent to:
For case 1:
'a', 2
'b', 1, 3
For case 2:
'a', 0, 3
'b', 2, 5
'c', 5
For case 3:
'a', 0
'b', 0
'c', 0
For case 4:
'a', 0, 3
'b', 0, 3, 5
'c', 0
BONUS (just for bragging rights) Make your code time efficient. That is can you minimise the number of operations it performs?
'b' 2, 1
be valid for the first case, or does it have to be in increasing order? \$\endgroup\$c
appears in the input, area
andb
guaranteed to appear at least once as well? \$\endgroup\$