I have a string
s1='abcdebcfg'
And for some reason the same string with added characters ('-','.')
s2='..abcde--bc-fg'
I want to map the index of a character from s1
to s2
Example:s1:0 -->s2:2 , s1:5 -->s2:9 ...
I have a string
s1='abcdebcfg'
And for some reason the same string with added characters ('-','.')
s2='..abcde--bc-fg'
I want to map the index of a character from s1
to s2
Example:s1:0 -->s2:2 , s1:5 -->s2:9 ...
I solved by :
counting the number of occurrence of the character in s1
at position i
and then find the character s1[i]
in s2
that has the same number of occurrence
def find_nth(needle,haystack, n):
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
for i in range(len(s1)) :
occurrence= s1[:i+1].count(s1[i])
j=find_nth(s1[i], s2, occurrence)
Note I found the find_nth here
You can try something like this:
for i in len(s1):
j = s2.find(s1[i])
print "s1:",i,"-->s2:",j
you may use two stack for each (s1,s2)with index as key and character as value then pop the values from each compare them and generate required output.
Ok, so something like this should work:
for i in range(len(s1)):
for j in range (i,len(s2)):
if s2[j]==s1[i]:
print "s1:",i,"-->s2:",j
break