Skip to content
BytePatterns

Spot Dictionary Words in a Text

EasyTries#trie#prefix-pruning~15m

Problem

Given a string text and a list of distinct words, return every pair [i, j] such that the slice of text from index i to index j, both included, is one of the words. Occurrences may overlap. Return the pairs sorted by i, then by j.

Examples

Input:  text = "bytesofcode", words = ["byte", "bytes", "code", "of", "so"]
Output: [[0, 3], [0, 4], [4, 5], [5, 6], [7, 10]]
Why:    byte, bytes, so, of and code, where so and of share the letter o
Input:  text = "ababa", words = ["aba", "ab"]
Output: [[0, 1], [0, 2], [2, 3], [2, 4]]
Input:  text = "abc", words = ["d"]
Output: []
Why:    edge case, no word occurs

Hints

0 / 3

Stuck on the idea rather than the code? Word Search With a Trie covers it.