Alphabetical line generation algorithm, which is alphabetically between two other lines?

The problem I'm trying to solve: given that you have two different lines consisting of lowercase letters from a to z, find the line between the two lines so that you can always find between the lines.

Additional Information:

Given that "a" precedes "b" in alphabetical order, there is an infinite number of lines between "a" and "b" when sorting like a dictionary would be: "aa", "aaa", "aaaa", 'ab', ' aba 'etc. However, there is no infinite number of lines between all lines - nothing happens between "a" and "aa". In addition, there is only one intermediate string 'aa' between 'a' and 'aaa'.

What is an algorithm that can find a string X that comes in alphabetical order between 'a' and 'b', which also satisfies the condition that there is an infinite number of lines between 'a' and X, as well as X and 'b'

+6
algorithm
source share
2 answers

assuming you can insert an infinite number of lines between two lines.

If the bottom line is shorter, add as many "a" to make the lengths equal, then add "b" to the middle line. If the top word is shorter, make the middle line equal to the bottom line and add z to the middle line. If two lines are of equal length, use either method.

+4
source share

You have indicated everything you need to know in order to find a solution. In principle, a finite number of lines exist only if one line is the prefix of the other, and the rest is the line "a" s.

Otherwise, you can find an infinite number of intermediate lines.

+1
source share

All Articles