Find all the k substring index range



Problem to find the string k starting and ending index of given string.



Example-1:


Input    : s = "ilovemycrazycodingiamcrazy"   k = "crazy"
Output   : [(7, 11), (21, 25)] 


Example-2:


Input    : s = "flightflowflower"   k = "fl"
Output   : [(0, 1), (6, 7), (10, 11)] 







Solution




s = "ilovemycrazycodingiamcrazy"

k = "crazy"

l = []

f = 0

if(k not in s):

    print(-1)

else:

    n = s.count(k)

    for i in range(n):

        a = s.index(k)

        b = a + (len(k))

        e = (b) + f

        l.append((a+f,e-1))

        d = s[b:]

        s = d

        f = e

print(l)



Output



[(7, 11), (21, 25)]