30
3
Given an input string consisting of only letters, return the step-size that results in the minimum amount of steps that are needed to visit all the letters in order over a wrapping alphabet, starting at any letter.
For example, take the word, dog
. If we use a step-size of 1, we end up with:
defghijklmnopqrstuvwxyzabcdefg Alphabet
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
defghijklmnopqrstuvwxyzabcdefg Visited letters
d o g Needed letters
For a total of 30 steps.
However, if we use a step-size of 11, we get:
defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg
^ ^ ^ ^ ^ ^
d o z k v g Visited letters
d o g Needed letters
For a total of 6 steps. This is the minimum amount of steps, so the return result for dog
is the step-size; 11
.
Test cases:
"dog" -> 11
"age" -> 6
"apple" -> 19
"alphabet" -> 9
"aaaaaaa" -> 0 for 0 indexed, 26 for 1 indexed
"abcdefga" -> 1 or 9
"aba" -> Any odd number except for 13
"ppcg" -> 15
"codegolf" -> 15
"testcase" -> 9
"z" -> Any number
"joking" -> 19
Rules
- Input will be a non-empty string or array of characters consisting only of the letters
a
toz
(you can choose between uppercase or lowercase) - Output can be 0 indexed (i.e. the range
0-25
) or 1 indexed (1-26
) - If there's a tie, you can output any step-size or all of them
- This is code-golf, so the lowest amount of bytes for each language wins!
Do we need to handle empty input? – pizzapants184 – 2018-12-12T04:32:36.593
1@pizzapants184 No. I've updated the question to specify the input will be non-empty – Jo King – 2018-12-12T04:33:05.827
Can we take input as an array of characters? – Shaggy – 2018-12-12T06:54:49.593
@Shaggy Sure you can – Jo King – 2018-12-12T07:58:48.157
Is there a reason this uses letters instead of numbers? – Post Rock Garf Hunter – 2018-12-12T13:54:26.847