0

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 ...

AWRAM
  • 333
  • 2
  • 16

4 Answers4

0

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

Community
  • 1
  • 1
AWRAM
  • 333
  • 2
  • 16
-1

You can try something like this:

    for i in len(s1):
       j = s2.find(s1[i])
       print "s1:",i,"-->s2:",j
Alessandro Gaballo
  • 708
  • 3
  • 13
  • 28
-1

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.

Deepak TS
  • 19
  • 3
-1

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
Alessandro Gaballo
  • 708
  • 3
  • 13
  • 28