Given a string text and an array of strings words, return an array of all index pairs [i, j] so that the substring text[i...j] is in words.
Return the pairs [i, j] in sorted order (i.e., sort them by their first coordinate, and in case of ties sort them by their second coordinate).
Example 1:
Input: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"] Output: [[3,7],[9,13],[10,17]]
Example 2:
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].
Constraints:
1 <= text.length <= 1001 <= words.length <= 201 <= words[i].length <= 50text and words[i] consist of lowercase English letters.words are unique.Problem Overview: You are given a string text and a list of words. The goal is to return all index pairs [i, j] such that the substring text[i...j] exists in the words list. The result must be sorted first by the starting index and then by the ending index.
Approach 1: Brute Force Substring Check (Time: O(n * m * k), Space: O(m * k))
The most direct solution checks every possible substring starting at each index of text. Store all words in a hash set for O(1) lookups. For each start index i, extend the substring character by character and check whether the current substring exists in the set. If it does, record the pair [i, j]. Here, n is the length of the text, m is the number of words, and k is the average word length. This approach is simple and works well when the word list is small, but repeated substring creation makes it inefficient for larger inputs. The solution mainly relies on operations over string processing and simple array traversal.
Approach 2: Trie-Based Prefix Matching (Time: O(n * L), Space: O(T))
A more efficient approach builds a Trie from all words in the dictionary. Each node represents a character and marks whether a word ends at that node. After building the Trie, iterate through the text starting at every index i. From that position, traverse the Trie while moving forward in the text. If a node representing the current prefix marks the end of a word, record the index pair [i, j]. Stop early if the current character does not exist in the Trie path.
This avoids generating every substring explicitly and instead matches prefixes incrementally. If L is the maximum word length, each starting index explores at most L characters, giving a time complexity of O(n * L). The Trie requires O(T) space where T is the total number of characters across all words. This approach is a classic application of the Trie data structure for multi-pattern matching. The output pairs are naturally generated in sorted order because you scan the text left-to-right and extend matches forward.
Approach 3: Sorted Words with Prefix Checks (Time: O(n * log m * L), Space: O(m))
Another option sorts the word list and checks prefixes using binary search. For each index in the text, you test substrings up to the maximum word length and check whether they exist in the sorted list. Sorting allows faster lookups compared to scanning the entire dictionary, but prefix checks still require substring construction. This method leverages sorting and binary search but is typically slower than the Trie approach for large dictionaries.
Recommended for interviews: The Trie-based solution is what interviewers usually expect. The brute force approach demonstrates that you understand the problem and can generate candidate substrings. Building a Trie shows stronger algorithmic thinking because it reduces redundant work and performs prefix matching efficiently across many words.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force with Hash Set | O(n * m * k) | O(m * k) | Small dictionary size or quick prototype solution |
| Trie-Based Matching | O(n * L) | O(T) | Best general solution when many words share prefixes |
| Sorted Words + Binary Search | O(n * log m * L) | O(m) | When dictionary is static and prefix checks rely on sorted lookup |
Leetcode 1065 Index Pairs of a String • Ren Zhang • 2,381 views views
Watch 6 more video solutions →Practice Index Pairs of a String with our built-in code editor and test cases.
Practice on FleetCode