1
+ '''
2
+ 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.
3
+
4
+
5
+
6
+ Example 1:
7
+
8
+ Input: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"]
9
+ Output: [[3,7],[9,13],[10,17]]
10
+ Example 2:
11
+
12
+ Input: text = "ababa", words = ["aba","ab"]
13
+ Output: [[0,1],[0,2],[2,3],[2,4]]
14
+ Explanation:
15
+ Notice that matches can overlap, see "aba" is found in [0,2] and [2,4].
16
+
17
+
18
+ Note:
19
+
20
+ All strings contains only lowercase English letters.
21
+ It's guaranteed that all strings in words are different.
22
+ 1 <= text.length <= 100
23
+ 1 <= words.length <= 20
24
+ 1 <= words[i].length <= 50
25
+ Return the pairs [i,j] in sorted order (i.e. sort them by their first coordinate in case of ties sort them by their second coordinate).
26
+ '''
27
+
28
+ class Solution (object ):
29
+ def indexPairs (self , text , words ):
30
+ """
31
+ :type text: str
32
+ :type words: List[str]
33
+ :rtype: List[List[int]]
34
+ """
35
+ if not words :
36
+ return []
37
+ result = []
38
+ for word in words :
39
+ starting = [index for index in range (len (text )) if text .startswith (word , index )]
40
+ for start in starting :
41
+ result .append ([start , start + len (word )- 1 ])
42
+ # print starting
43
+ result .sort ()
44
+ return result
45
+
0 commit comments