Suppose you have a sorted List containing server names. You’d like to collapse them as tightly as possible.
Example:
abcd01c, abcd02c, abcd04c, abcd05, z1x
should become
abcd0[1-4]c,abcd05,z1x
What is the simplest algorithm to take care of something like this?
I would store all strings in a prefix map, which makes the decision of a String exists very easy, and also allows fast iteration of a subset of Strings.
Store the Strings as:
The number is the count of characters which have to be taken from the previous String. This is a common implementation for dictionaries like phonebooks, where you have to store many similar Strings.
A Trie is a similar structure, as Brian Roach noticed in the comments.