My interpretation of your question: Look at the keys of the hash in the order of the values of the hash and join the keys (as strings) with overlaps.
Under this assumption the steps are:
- Sort the keys by the values.
- Intialize your merged string with the first key.
- Determine the maximum overlap of the merged string with the next key. Start with the maximum overlapping length, use substr to retrieve the end of the merged string and the beginning of the next key and compare them. Reduce overlap until a match is found and append the rest to the merged string. If no match is found, just concatenate the two strings.
- Repeat with all keys.