The Index Pairs of a String coding problem asks you to find all occurrences of a set of words within a given text. For each match, you should return the starting and ending indices [i, j]. The final list of pairs must be sorted first by the starting index and then by the ending index.
Amazon use this "Easy" question to test string search foundations. It evaluates your ability to find substrings and handle overlapping matches (e.g., if "aba" and "ab" are both words, they both might match starting at the same index). It also checks your sorting skills for the final output.
This is a String Matching problem. There are two common ways to solve it:
text.indexOf(word) in a loop to find all occurrences.words into a Trie.text:
text[i].text = "ababa", words = ["aba", "ab"]
[[0, 1], [0, 2], [2, 3], [2, 4]].indexOf logic is faster and easier to implement.Tries are the standard tool for "Multiple Pattern Matching." If you have many words to find in one text, using a Trie to check all words simultaneously for each starting position is much more efficient than searching for each word individually.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Longest Word in Dictionary | Medium | Solve | |
| Reorder Data in Log Files | Medium | Solve | |
| Word Abbreviation | Hard | Solve | |
| Words Within Two Edits of Dictionary | Medium | Solve | |
| Longest Common Suffix Queries | Hard | Solve |