Index Pairs of a String - Problem
Index Pairs of a String
You're a text editor developer building a word highlighting feature. Given a document text and a dictionary of
Task: Return an array of index pairs
Important: Results must be sorted by start position first, then by end position if there are ties. This ensures consistent highlighting order in your text editor.
Example: If text =
You're a text editor developer building a word highlighting feature. Given a document text and a dictionary of
words to highlight, you need to find all occurrences of these words in the text and return their positions.Task: Return an array of index pairs
[i, j] where each pair represents the start and end positions of a word from the dictionary found in the text. The substring text[i...j] (inclusive) must exactly match one of the words.Important: Results must be sorted by start position first, then by end position if there are ties. This ensures consistent highlighting order in your text editor.
Example: If text =
"thestoryofleetcodeandme" and words = ["story", "fleet", "leetcode"], you should find "story" at positions [3,7], "leetcode" at [8,15], creating pairs [[3,7], [8,15]]. Input & Output
example_1.py โ Basic Case
$
Input:
text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"]
โบ
Output:
[[3,7],[9,12],[10,17]]
๐ก Note:
Found "story" at positions 3-7, "fleet" at 9-12, and "leetcode" at 10-17. Note that "fleet" and "leetcode" overlap, which is allowed.
example_2.py โ Single Character
$
Input:
text = "ababa", words = ["aba","ab"]
โบ
Output:
[[0,1],[0,2],[2,3],[2,4]]
๐ก Note:
Found "ab" at [0,1] and [2,3], "aba" at [0,2] and [2,4]. Multiple overlapping matches are included.
example_3.py โ No Matches
$
Input:
text = "wordgoodsword", words = ["word","good","best"]
โบ
Output:
[[0,3],[4,7],[8,11]]
๐ก Note:
Found "word" at [0,3] and [8,11], "good" at [4,7]. "best" is not found in the text.
Visualization
Tap to expand
Understanding the Visualization
1
Build Search Index
Create a Trie (prefix tree) from all keywords you want to highlight
2
Scan Document
Move through the text character by character, starting fresh searches at each position
3
Follow Paths
At each position, follow the Trie paths as far as possible, collecting complete word matches
4
Mark Highlights
Record the start and end positions of each found keyword for the highlighting system
Key Takeaway
๐ฏ Key Insight: The Trie data structure enables us to search for multiple patterns simultaneously in a single pass, making it the optimal solution for multi-pattern string matching problems.
Time & Space Complexity
Time Complexity
O(n ร L + m ร L)
O(n ร L) to scan text ร max word length + O(m ร L) to build Trie
โ Linear Growth
Space Complexity
O(m ร L + k)
O(m ร L) for Trie nodes + O(k) for result pairs
โ Linear Space
Constraints
- 1 โค words.length โค 20
- 1 โค words[i].length โค text.length โค 100
- text and words[i] consist of lowercase English letters only
- All the strings of words are unique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code