Leetcode 1065 Index Pairs of a String

Given a text string and words (a list of strings), return all index pairs [i, j] so that the substring text[i]…text[j] is in the list of words.

Input: text = “thestoryofleetcodeandme”, words = [“story”,”fleet”,”leetcode”]
Output: [[3,7],[9,13],[10,17]]
Input: text = “ababa”, words = [“aba”,”ab”]
Output: [[0,1],[0,2],[2,3],[2,4]]
Explanation: 
Notice that matches can overlap, see “aba” is found in [0,2] and [2,4].
  • Çözülecek
    def indexPairs(self, text, words):
        ans = []
        for word in words:
            temp = text
            ind = temp.find(word)
            count = 0
            while ind != -1:
                ans.append([ind, ind+len(word)-1])
                ind = temp.find(word, ind+1)
        return sorted(ans)