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 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
Smart Text Editor: Multi-Keyword HighlighterDocument Text:"thestoryofleetcodeandme"Step 1: Build Trie from Keywordsrootslf"story""leetcode""fleet"Step 2-4: Scan & MatchPosition 3: Found "story" [3,7]storyPosition 9: Found "fleet" [9,12]Position 10: Found "leetcode" [10,17]fleetleetcodeResult: [[3,7], [9,12], [10,17]]โœ“ Efficient O(nร—L) scanning โœ“ Natural sorting order โœ“ Handles overlapping matches
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

n
2n
โœ“ Linear Growth
Space Complexity
O(m ร— L + k)

O(m ร— L) for Trie nodes + O(k) for result pairs

n
2n
โœ“ 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
Asked in
Google 42 Amazon 38 Microsoft 29 Meta 24
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen