I have a two strings:
string1 - hello how are you
,
String2 - olo
(including space character)
Output: lo ho
( hello
I would calculate positions of characters from string2
within string1
and then pick the permutation with minimum distance between lowest and highest character position:
# positions are:
# 01234567890123456
string1 = 'hello how are you'
string2 = 'olo'
# get string1 positions for each character from set(string2)
positions = {'o': [4, 7, 15],
'l': [2, 3]}
# get all permutations of positions (don't repeat the same element)
# then pick the permutation with minimum distance between min and max position
# (obviously, this part can be optimized, this is just an illustration)
permutations = positions['o'] * positions['l'] * positions['o']
permutations = [[4,2,7], [4,3,7], [4,2,15], ...]
the_permutation = [4,3,7]
# voilà
output = string1_without_spaces[3:7]